From: John Hodge Date: Tue, 30 Nov 2010 13:45:00 +0000 (+0800) Subject: Start on a ncurses GUI X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=a7c0580c1592fe19ef18ce5b86b0088c835be030;p=tpg%2Fopendispense2.git Start on a ncurses GUI > Very sparcely commented atm, and really needs to be sorted out, but it renders. --- diff --git a/items.cfg b/items.cfg index 21e09ef..46197b8 100644 --- a/items.cfg +++ b/items.cfg @@ -13,6 +13,8 @@ coke 6 96 Coke # Pseudo items pseudo 0 128 clue # clue.flac pseudo 1 10 laserprint # print 10 pages +pseudo 2 15 phone # Ring Ring +pseudo 3 2500 membership # here comes the money! # Snack machine snack 13 128 Smiths Salt & Vinegar diff --git a/src/client/Makefile b/src/client/Makefile index 01a8c92..8c376c3 100644 --- a/src/client/Makefile +++ b/src/client/Makefile @@ -1,6 +1,6 @@ CFLAGS := -Wall -Werror -g -LDFLAGS := -g +LDFLAGS := -g -lncurses BIN := ../../dispense OBJ := main.o diff --git a/src/client/main.c b/src/client/main.c index ee93227..5f5f1c8 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -14,6 +14,7 @@ #include // isspace #include #include +#include #include // close #include // gethostbyname @@ -30,6 +31,8 @@ typedef struct sItem { } tItem; // === PROTOTYPES === + int ShowNCursesUI(void); + int sendf(int Socket, const char *Format, ...); int OpenConnection(const char *Host, int Port); void Authenticate(int Socket); @@ -159,6 +162,10 @@ int main(int argc, char *argv[]) // - Hmm... that would require standardising the item ID to be : // Oh, why not :) + #if 1 + i = ShowNCursesUI(); + #else + for(;;) { char *buf; @@ -181,36 +188,40 @@ int main(int argc, char *argv[]) printf("Bad item (should be between 0 and %i)\n", giNumItems); continue; } - - sendf(sock, "DISPENSE %s\n", gaItems[i].Ident); - - len = recv(sock, buffer, BUFSIZ-1, 0); - buffer[len] = '\0'; - trim(buffer); - - responseCode = atoi(buffer); - switch( responseCode ) - { - case 200: - printf("Dispense OK\n"); - break; - case 401: - printf("Not authenticated\n"); - break; - case 402: - printf("Insufficient balance\n"); - break; - case 406: - printf("Bad item name, bug report\n"); - break; - case 500: - printf("Item failed to dispense, is the slot empty?\n"); - break; - default: - printf("Unknown response code %i\n", responseCode); - break; - } - + break; + } + } + #endif + + if( i >= 0 ) + { + // Dispense! + sendf(sock, "DISPENSE %s\n", gaItems[i].Ident); + + len = recv(sock, buffer, BUFSIZ-1, 0); + buffer[len] = '\0'; + trim(buffer); + + responseCode = atoi(buffer); + switch( responseCode ) + { + case 200: + printf("Dispense OK\n"); + break; + case 401: + printf("Not authenticated\n"); + break; + case 402: + printf("Insufficient balance\n"); + break; + case 406: + printf("Bad item name, bug report\n"); + break; + case 500: + printf("Item failed to dispense, is the slot empty?\n"); + break; + default: + printf("Unknown response code %i\n", responseCode); break; } } @@ -220,6 +231,160 @@ int main(int argc, char *argv[]) return 0; } +/** + */ +int ShowNCursesUI(void) +{ + int ch; + int i, times; + int xBase, yBase; + const int displayMinWidth = 34; + const int displayMinItems = 8; + char *titleString = "Dispense"; + int titleStringLen = strlen(titleString); + int itemCount = displayMinItems; + int itemBase = 0; + + int height = itemCount + 3; + int width = displayMinWidth; + + // Enter curses mode + initscr(); + raw(); noecho(); + + xBase = COLS/2 - width/2; + yBase = LINES/2 - height/2; + + for( ;; ) + { + // Header + move( yBase, xBase ); + addch('/'); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch(' '); + addstr(titleString); + addch(' '); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch('\\'); + + // Items + for( i = 0; i < itemCount; i ++ ) + { + int _x, _y; + move( yBase + 1 + i, xBase ); + addch('|'); + addch(' '); + + // Check for ... row + if( i == 0 && itemBase > 0 ) { + printw(" ..."); + times = width - 1 - 8; + while(times--) addch(' '); + } + else if( i == itemCount - 1 && itemBase < giNumItems - itemCount ) { + printw(" ..."); + times = width - 1 - 8; + while(times--) addch(' '); + } + // Show an item + else { + if( itemBase + i < 0 || itemBase + i >= giNumItems ) { + printw("%02i %i OOR", itemBase + i, i); + continue ; + } + printw("%02i %s", itemBase + i, gaItems[itemBase + i].Desc); + + getyx(stdscr, _y, _x); + times = width - 6 - (_x - xBase); // TODO: Better handling for large prices + while(times--) addch(' '); + printw("%4i ", gaItems[itemBase + i].Price); + } + + // Scrollbar (if needed) + if( giNumItems > itemCount ) { + if( i == 0 ) { + addch('A'); + } + else if( i == itemCount - 1 ) { + addch('V'); + } + else { + int percentage = itemBase * 100 / (giNumItems-itemCount); + if( i-1 == percentage*(itemCount-3)/100 ) { + addch('#'); + } + else { + addch('|'); + } + } + } + else { + addch('|'); + } + } + + // Footer + move( yBase + 1 + itemCount, xBase ); + addch('\\'); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch(' '); + addstr(titleString); + addch(' '); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch('/'); + + move( yBase + 1 + itemCount + 1, xBase ); + { + int count = itemCount-2; + int ofs = itemBase; + if( itemBase == 0 ) count ++; + else ofs ++; + if( itemBase == giNumItems-itemCount) { + count ++; + ofs ++; + } + printw("%i - %i / %i items", itemBase, itemBase+count, giNumItems); + } + + ch = getch(); + + if( ch == '\x1B' ) { + ch = getch(); + if( ch == '[' ) { + ch = getch(); + + switch(ch) + { + case 'B': + if( itemBase < giNumItems - (itemCount) ) + itemBase ++; + break; + case 'A': + if( itemBase > 0 ) + itemBase --; + break; + } + } + else { + + } + } + else { + break; + } + + } + + + // Leave + endwin(); + return -1; +} + // === HELPERS === int sendf(int Socket, const char *Format, ...) { diff --git a/ui.txt b/ui.txt index 8b1cd95..44720d0 100644 --- a/ui.txt +++ b/ui.txt @@ -3,6 +3,7 @@ Client UI Design Left/Right changes the item view (between Drink, Pseudo and Snack - and others if defined) Should the width of the display be fixed? +> Current width is 34 characters Howabout the height? @@ -33,7 +34,7 @@ Mockup: Snacks /----------- Dispense -----------\ - | 00 Smith's Salt+Vinegar 72 A + | 00 Smith's Salt+Vinegar 128 A | 01 Unused 0 # | 02 Something 0 | | 03 Something 0 |