parent
8417c8f438
commit
e51885a007
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
gcc main.c -o menu -lncurses
|
||||
|
||||
./menu
|
@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
gcc main.c -o menu -lncurses
|
||||
g++ main.cpp -o menu -lncurses
|
||||
|
||||
./menu
|
||||
|
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
g++ main.cpp -o menu -lncurses
|
||||
|
||||
./menu
|
@ -0,0 +1,53 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
#define ctrl(x) (x & 0x1F) // Detection of ctrl+x presses please do not forget that in unix ctrl+c and ctrl+z are special commands.
|
||||
|
||||
|
||||
int main(int argv, char ** argc)
|
||||
{
|
||||
//Init Ncurses
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
|
||||
if(has_colors() == FALSE)
|
||||
{
|
||||
printf("Colors cnat be used in this terminal\n");
|
||||
endwin();
|
||||
}
|
||||
|
||||
start_color();
|
||||
|
||||
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
|
||||
init_pair(2, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(3, COLOR_BLUE, COLOR_WHITE);
|
||||
|
||||
// You can cumulate attributes in this speacil variable
|
||||
attr_t emphasis = A_REVERSE | COLOR_PAIR(1);
|
||||
attr_t console = A_REVERSE | COLOR_PAIR(3);
|
||||
// Hee is the example use case if that given variable
|
||||
attron(emphasis);
|
||||
mvprintw(1,0,"emphasis with reversed color pair 1");
|
||||
attroff(emphasis);
|
||||
|
||||
char c;
|
||||
//Nwo if we want to create a char with attirbites on it ?
|
||||
chtype cht = 'a' | A_UNDERLINE | COLOR_PAIR(2);
|
||||
|
||||
attron(console);
|
||||
mvprintw(4,0,"Every char for ncurses is a chtype");
|
||||
attroff(console);
|
||||
mvaddch(5,0,cht);
|
||||
|
||||
while(c = getch())
|
||||
{
|
||||
attron(console);
|
||||
mvprintw(10,0,"KEY Name : %s - 0x%02x\n", keyname(c), c);
|
||||
if(c == ctrl('a'))
|
||||
{
|
||||
mvprintw(9,0,"ctrl-a detected");
|
||||
}
|
||||
}
|
||||
|
||||
endwin();
|
||||
}
|
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
g++ main.cpp -o menu -lncurses
|
||||
|
||||
./menu
|
@ -0,0 +1,32 @@
|
||||
#include <ncurses.h>
|
||||
#include "player.hpp"
|
||||
|
||||
int main(int argv, char ** argc)
|
||||
{
|
||||
//variables
|
||||
int i,std_xMax, std_yMax = 0;
|
||||
|
||||
//Init Ncurses
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
|
||||
getmaxyx(stdscr, std_yMax, std_xMax);
|
||||
|
||||
WINDOW * play_win = newwin(20, 50, std_yMax/2-10, 10);
|
||||
keypad(play_win, true); // so that we can use the arrow keys
|
||||
box(play_win,0,0);
|
||||
|
||||
Player *player = new Player(play_win, 1, 1, '#');
|
||||
printw("Press x to quit");
|
||||
printw("\nPress arrow keys to move");
|
||||
refresh();
|
||||
wrefresh(play_win);
|
||||
|
||||
do{
|
||||
player->display();
|
||||
wrefresh(play_win);
|
||||
}while(player->getMv()!='x');
|
||||
|
||||
endwin();
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
#ifndef _PLAYER_H_
|
||||
#define _PLAYER_H_
|
||||
|
||||
class Player
|
||||
{
|
||||
public:
|
||||
Player(WINDOW* win, int y, int x, char c);
|
||||
void mvUp();
|
||||
void mvDown();
|
||||
void mvLeft();
|
||||
void mvRight();
|
||||
int getMv();
|
||||
void display();
|
||||
|
||||
private:
|
||||
int xLoc, yLoc, xMax, yMax;
|
||||
char playerChar;
|
||||
WINDOW * curWin;
|
||||
};
|
||||
|
||||
|
||||
Player::Player(WINDOW* win, int y, int x, char c)
|
||||
{
|
||||
curWin = win;
|
||||
xLoc = x;
|
||||
yLoc = y;
|
||||
getmaxyx(curWin, yMax, xMax);
|
||||
keypad(curWin, true);
|
||||
playerChar = c;
|
||||
}
|
||||
|
||||
void Player::mvUp()
|
||||
{
|
||||
mvwaddch(curWin,yLoc, xLoc, '.');
|
||||
yLoc --;
|
||||
if(yLoc < 1)
|
||||
{
|
||||
yLoc = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::mvDown()
|
||||
{
|
||||
mvwaddch(curWin,yLoc, xLoc, '.');
|
||||
yLoc ++;
|
||||
if(yLoc > yMax-2)
|
||||
{
|
||||
yLoc = yMax-2;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::mvLeft()
|
||||
{
|
||||
mvwaddch(curWin,yLoc, xLoc, '.');
|
||||
xLoc --;
|
||||
if(xLoc < 1)
|
||||
{
|
||||
xLoc = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::mvRight()
|
||||
{
|
||||
mvwaddch(curWin,yLoc, xLoc, '.');
|
||||
xLoc ++;
|
||||
if(xLoc > xMax-2)
|
||||
{
|
||||
xLoc = xMax-2;
|
||||
}
|
||||
}
|
||||
|
||||
int Player::getMv()
|
||||
{
|
||||
int choice = wgetch(curWin);
|
||||
switch(choice)
|
||||
{
|
||||
case KEY_UP:
|
||||
mvUp();
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
mvDown();
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
mvLeft();
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
mvRight();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return choice;
|
||||
}
|
||||
|
||||
void Player::display()
|
||||
{
|
||||
mvwaddch(curWin,yLoc, xLoc, playerChar);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -1,11 +0,0 @@
|
||||
#!/bin/bash
|
||||
sudo apt install apt-transport-https curl
|
||||
|
||||
sudo curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg
|
||||
|
||||
echo "deb [signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg arch=amd64] https://brave-browser-apt-release.s3.brave.com/ stable main"|sudo tee /etc/apt/sources.list.d/brave-browser-release.list
|
||||
|
||||
sudo apt update
|
||||
|
||||
sudo apt install brave-browser
|
||||
|
@ -0,0 +1,58 @@
|
||||
#include <ncurses.h>
|
||||
#include "menu.h"
|
||||
|
||||
#define ctrl(x) (x & 0x1F) // Detection of ctrl+x presses please do not forget that in unix ctrl+c and ctrl+z are special commands.
|
||||
|
||||
// INFO: wgetch(win); or getch(); // getch will make a refresh for us.
|
||||
|
||||
int main(int argv, char ** argc)
|
||||
{
|
||||
//Variables
|
||||
int xMax, yMax = 0;
|
||||
char pressed;
|
||||
|
||||
|
||||
//Init Ncurses
|
||||
initscr();
|
||||
|
||||
if(has_colors() == false) // If the terminal does have colors
|
||||
{
|
||||
printf("This menu is not supported by terminals without colors\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
start_color();
|
||||
|
||||
noecho();
|
||||
curs_set(0); // Cursor won't show up
|
||||
getmaxyx(stdscr, yMax, xMax);
|
||||
// size y size x pos y pos x
|
||||
WINDOW *win = newwin(yMax/2, xMax/2, yMax/4, xMax/4);
|
||||
box(win,0,0);
|
||||
|
||||
//Setc colors
|
||||
init_pair(1, COLOR_WHITE, COLOR_BLUE);
|
||||
|
||||
std::string menu1[4]={"New","Open","Save","Exit"};
|
||||
std::string menu2[3]={"Copy","Cut","Paste"};
|
||||
std::string menu3[5]={"Terminal","Help","Info","Update","Manual"};
|
||||
|
||||
// Second stage declarations
|
||||
Menu menus[3]={ // init the menus
|
||||
Menu("File", 'f', menu1, 4),
|
||||
Menu("Edit", 'e', menu2, 3),
|
||||
Menu("Options", 'o', menu3, 5),
|
||||
};
|
||||
|
||||
MenuBar menuBar = MenuBar(win, menus, 3); // init the menu bar
|
||||
menuBar.draw();
|
||||
|
||||
while(pressed = wgetch(win))
|
||||
{
|
||||
menuBar.handleTrigger(pressed);
|
||||
menuBar.draw();
|
||||
}
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
Binary file not shown.
@ -0,0 +1,180 @@
|
||||
#include <curses.h>
|
||||
#include <string>
|
||||
|
||||
#ifndef _MENU_H_
|
||||
#define _MENU_H_
|
||||
|
||||
class Menu
|
||||
{
|
||||
public:
|
||||
Menu(std::string name, char trigger, std::string *items, int itemCount);
|
||||
void selectNextItem();
|
||||
void selectPrevItem();
|
||||
|
||||
int startX, startY;
|
||||
std::string name;
|
||||
char trigger;
|
||||
int selectedItem = 0; // alyas have the first one selected
|
||||
std::string *items;
|
||||
int itemCount=0;
|
||||
};
|
||||
|
||||
Menu::Menu(std::string name, char trigger, std::string *items, int itemCount)
|
||||
{
|
||||
this->name = name;
|
||||
this->trigger = trigger;
|
||||
this->items = items;
|
||||
this->itemCount = itemCount;
|
||||
}
|
||||
|
||||
void Menu::selectNextItem()
|
||||
{
|
||||
selectedItem ++;
|
||||
if(selectedItem >= itemCount)
|
||||
{
|
||||
selectedItem = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::selectPrevItem()
|
||||
{
|
||||
selectedItem--;
|
||||
if(selectedItem < 0)
|
||||
{
|
||||
selectedItem = itemCount-1;
|
||||
}
|
||||
}
|
||||
|
||||
class MenuBar
|
||||
{
|
||||
public:
|
||||
MenuBar(WINDOW * win, Menu * menus, int menuCnt);
|
||||
void draw();
|
||||
void handleTrigger(char trigger);
|
||||
void reset();
|
||||
void drawMenu(Menu menu, int currIndex);
|
||||
void drawMenuItems(Menu menu);
|
||||
|
||||
WINDOW * win;
|
||||
WINDOW * menuwin;
|
||||
Menu * menus;
|
||||
int menuCnt;
|
||||
int i =0;
|
||||
int selected_menu = 90;
|
||||
int start_pos = 0;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
MenuBar::MenuBar(WINDOW * win, Menu * menus, int menuCnt)
|
||||
{
|
||||
this->win = win;
|
||||
this->menus = menus;
|
||||
this->menuCnt = menuCnt;
|
||||
int currPos = 2;
|
||||
|
||||
//Initialize the menu window
|
||||
int xMax, yMax, xBeg, yBeg = 0;
|
||||
getmaxyx(win, yMax, xMax);
|
||||
getbegyx(win, yBeg, xBeg);
|
||||
|
||||
menuwin = newwin(yMax-2, xMax-2, yBeg+1, xBeg+1);
|
||||
keypad(menuwin, true);
|
||||
wrefresh(menuwin);
|
||||
|
||||
for(i = 0; i < menuCnt ; i ++)
|
||||
{
|
||||
this->menus[i].startX = currPos;
|
||||
currPos += this->menus[i].name.length() + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::draw()
|
||||
{
|
||||
for(i = 0; i < menuCnt ; i ++)
|
||||
{
|
||||
drawMenu(menus[i], i);
|
||||
}
|
||||
selected_menu = -1;
|
||||
}
|
||||
|
||||
void MenuBar::drawMenu(Menu menu,int currIndex)
|
||||
{
|
||||
start_pos = menu.startX;
|
||||
name = menu.name;
|
||||
if(currIndex == selected_menu)
|
||||
{
|
||||
wattron(win, A_STANDOUT);
|
||||
}
|
||||
mvwprintw(win, 0, start_pos, name.c_str());
|
||||
wattroff(win, A_STANDOUT);
|
||||
wrefresh(win);
|
||||
|
||||
int ch = 0;
|
||||
|
||||
drawMenuItems(menu);
|
||||
|
||||
while(currIndex == selected_menu && (ch = wgetch(menuwin)))
|
||||
{
|
||||
switch(ch)
|
||||
{
|
||||
case KEY_UP:
|
||||
menu.selectPrevItem();
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
menu.selectNextItem();
|
||||
break;
|
||||
default:
|
||||
currIndex = -1;
|
||||
break;
|
||||
}
|
||||
drawMenuItems(menu);
|
||||
}
|
||||
werase(menuwin);
|
||||
wrefresh(menuwin);
|
||||
reset();
|
||||
}
|
||||
|
||||
void MenuBar::drawMenuItems(Menu menu)
|
||||
{
|
||||
int yMax, xMax;
|
||||
getmaxyx(menuwin, yMax, xMax);
|
||||
|
||||
for(i=0; i < menu.itemCount ; i ++)
|
||||
{
|
||||
mvwprintw(menuwin,i,0,menu.items[i].c_str());
|
||||
if(menu.selectedItem == i)
|
||||
{
|
||||
//if selected change to color pair 1
|
||||
mvwchgat(menuwin,i,0,xMax,A_NORMAL,1, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not selected but we still want it to be reversed
|
||||
mvwchgat(menuwin,i,0,xMax,A_STANDOUT,0, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::reset()
|
||||
{
|
||||
for(i = 0; i < menuCnt ; i ++)
|
||||
{
|
||||
start_pos = this->menus[i].startX;
|
||||
name = this->menus[i].name;
|
||||
mvwprintw(win, 0, start_pos, name.c_str());
|
||||
}
|
||||
wrefresh(win);
|
||||
}
|
||||
|
||||
void MenuBar::handleTrigger(char trigger)
|
||||
{
|
||||
for(i = 0; i < menuCnt ; i ++)
|
||||
{
|
||||
if(trigger == this->menus[i].trigger)
|
||||
{
|
||||
selected_menu = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in new issue