Ejemplo n.º 1
0
SDL_Texture* loadTexture(const std::string &filename, SDL_Renderer *ren)
{
    std::string imagePath = GUI_getResourcePath() + filename;
    
    GUI_Log("Load:%s\n", imagePath.c_str() );
    SDL_Texture *tex = IMG_LoadTexture(ren, imagePath.c_str());
    if (tex == NULL){
        GUI_Log( "Load image %s failed.", filename.c_str() );
    }
    return tex;
}
Ejemplo n.º 2
0
void GUI_Log1(const char *s, int p0) {
  char ac[50 + 10];
  char* sOut = ac;
  strncpy(ac, s, 49);
  sOut += strlen(sOut);
  GUI__AddSpaceHex(p0, 8, &sOut);
  GUI_Log(ac);
}
Ejemplo n.º 3
0
void GUI_Log3(const char *s, int p0, int p1, int p2) {
  char ac[50 + 30];
  char* sOut = ac;
  strncpy(ac, s, 49);
  sOut += strlen(sOut);
  GUI__AddSpaceHex(p0, 8, &sOut);
  GUI__AddSpaceHex(p1, 8, &sOut);
  GUI__AddSpaceHex(p2, 8, &sOut);
  GUI_Log(ac);
}
Ejemplo n.º 4
0
Uint32 SavePaletteCallback( Uint32 interval, void *param)
{
    new GUI_FileDialog( topWin, true, "*.txt", "Untitled.txt", [](const char *fn) -> bool {
        GUI_Log( "File Save Pal: %s\n", fn );
        savePalette( fn );
        return true;
    });

    return 0;
}
Ejemplo n.º 5
0
Uint32 LoadPaletteCallback( Uint32 interval, void *param)
{
    new GUI_FileDialog( topWin, false, "*.txt", "Untitled.txt",  [](const char *fn) -> bool {
        GUI_Log( "File Load Pal: %s\n", fn );
        loadPalette( fn );
        for( int i=0; i<numColor; i++ ) {
            c[i]->color = mainPalette[activePalette][i];
        }
        return true;
    });
    
    return 0;
}
Ejemplo n.º 6
0
void OpenColor( void ) {
    if( colorDlg == NULL ) {
        colorDlg = new GUI_ColorDialog( topWin );
        colorDlg->selected_change = [](GUI_ColorDialog *d, int pantoneCode, SDL_Color pantoneColor )
        {
            GUI_Log( "Color: %i (%i,%i,%i)\n", pantoneCode, pantoneColor.r, pantoneColor.g, pantoneColor.b );
            mainPalette[activePalette][activeColor] = pantoneColor;
            c[activeColor]->color = pantoneColor;
        };
        colorDlg->onClose = [](GUI_WinBase *w) -> bool {
            colorDlg = NULL;
            return true;
        };
    }
    else {
        if( colorDlg->isVisible() )
            colorDlg->hide();
        else
            colorDlg->show();
    }
    colorDlg->setActiveColor( mainPalette[activePalette][activeColor] );
}
Ejemplo n.º 7
0
void GUI_TemplateDialog::draw()
{
    if( bNeedUpdate ) {
        bNeedUpdate = false;
        
        GUI_Log( ">> Dir: %s\n", cur_wdir );
        if( fileEdit ) {
            delete fileEdit;
        }
        fileEdit = new GUI_EditText( this, "", 5, 3, tw_area.w-60 );
        
        if( bttnOK ) {
            delete bttnOK;
        }
        bttnOK = new GUI_Button( this, "Open", tw_area.w-52, 4, 48, 30, cGrey,
                                [](GUI_Button *b) {
                                    GUI_TemplateDialog *dlg = (GUI_TemplateDialog *)b->parent->parent;
                                    if( dlg->fileEdit->title_str && dlg->fileEdit->title_str[0] ) {
                                        //getcwd(dlg->cur_wdir,512);
                                        strcat(dlg->cur_wdir, "/" );
                                        strcat(dlg->cur_wdir, dlg->fileEdit->title_str );
                                        GUI_Log( "File: %s\n",dlg->cur_wdir );
                                        if( dlg->file_cmd ) {
                                            if( dlg->file_cmd( dlg->cur_wdir ) )
                                                dlg->close();
                                        }
                                    }
                                });
        
        DIR *dp;
        if (!(dp=opendir(cur_wdir))) {
            GUI_Log("wdir not accessable");
            return;
        }
        dirent *dir;
        
        int str_index = 0;
        int str_count = 0;
        int file_count = 0;
        while ((dir=readdir(dp))!=0) {
            if (!dir->d_ino) {
                GUI_Log("d_ino?");
                continue;
            }
            switch (dir->d_type) {
                case DT_DIR:
                    //GUI_Log( "[%s]\n", dir->d_name );
                    break;
                case DT_REG:
                    GUI_Log( "%s\n", dir->d_name );
                    if( dir->d_name[0] != '.' ) {
                        if( str_index + strlen(dir->d_name) < max_dirs-1 ) {
                            if( fileExtension == NULL || (strlen( dir->d_name ) >= strlen( fileExtension) ) ) {
                                if( fileExtension == NULL || !strcmp( dir->d_name +strlen( dir->d_name)-strlen( fileExtension ), fileExtension ) ) {
                                    fileList[file_count++] = &dirs[str_index];
                                    strcpy( &dirs[str_index], dir->d_name );
                                    str_index += strlen(dir->d_name)+1;
                                }
                                else {
                                    GUI_Log( "no %s %s\n", dir->d_name, fileExtension );
                                }
                            }
                            else {
                                GUI_Log( "Short %i %i\n", strlen( &dirs[str_index]), strlen( fileExtension) );
                            }
                        }
                    }
                    break;
                case DT_LNK:
                    GUI_Log( "%s\n", dir->d_name );
                    break;
            }
            if( str_count >= max_dir-1 )
                break;
            if( file_count >= max_dir-1 )
                break;
        }
        fileList[file_count] = NULL;
        closedir( dp );
        
        int by = 38;
        if( fileListBox ) {
            delete fileListBox;
        }
        fileListBox = new GUI_List( this, (const char **)fileList, file_count, 180, by-1, tw_area.w-180-2, this->tw_area.h-titleBarSize+1-by-1, 0, false,
                                   [](GUI_List *l,const char *sel, int index) {
                                       if( index == l->selectedIndex ) {
                                           GUI_TemplateDialog *dlg = (GUI_TemplateDialog *)l->parent->parent;
                                           //getcwd(dlg->cur_wdir,512);
                                           //strcat( dlg->cur_wdir, "/" );
                                           //strcat( dlg->cur_wdir, sel );
                                           //GUI_Log( "File: %s\n", dlg->cur_wdir );
                                           if( dlg->fileEdit ) {
                                               dlg->fileEdit->setText( sel );
                                           }
                                           
                                       }
                                   }
                                   );
        fileListBox->title_str = "File List Box";

    }
    GUI_Dialog::draw();
    
}
Ejemplo n.º 8
0
void GUI_FileDialog::draw()
{
    if( bNeedUpdate ) {
        bNeedUpdate = false;
        
        GUI_Log( ">> Dir: %s\n", cur_wdir );
        
        const char *tt;
        if( bSaveDlg ) {
            tt = "Save";
        }
        else {
            tt = "Open";
        }
        if( fileEdit ) {
            delete fileEdit;
        }
        fileEdit = new GUI_EditText( this, "", 5, 3, tw_area.w-60 );
        if( defaultFilename )
            fileEdit->setText( defaultFilename );
        
        if( bttnOK ) {
            delete bttnOK;
        }
        bttnOK = new GUI_Button( this, tt, tw_area.w-52, 4, 48, 30, cGrey,
                                [](GUI_Button *b) {
                                    GUI_FileDialog *dlg = (GUI_FileDialog *)b->parent->parent;
                                    if( dlg->fileEdit->title_str && dlg->fileEdit->title_str[0] ) {
                                        //getcwd(dlg->cur_wdir,512);
                                        strcat(dlg->cur_wdir, "/" );
                                        strcat(dlg->cur_wdir, dlg->fileEdit->title_str );
                                        if( dlg->fileExtension ) {
                                            if( strcmp( &dlg->cur_wdir[strlen(dlg->cur_wdir)-strlen(dlg->fileExtension)], dlg->fileExtension ) ) {
                                                strcat( dlg->cur_wdir, dlg->fileExtension );
                                            }
                                        }
                                        GUI_Log( "File: %s\n",dlg->cur_wdir );
                                        if( dlg->file_cmd ) {
                                            if( dlg->file_cmd( dlg->cur_wdir ) )
                                                dlg->close();
                                        }
                                    }
                                });
        if( !bSaveDlg ) {
            fileEdit->disable = true;
            fileEdit->titleColor = cGrey;
        }
        
        DIR *dp;
        if (!(dp=opendir(cur_wdir))) {
            GUI_Log("wdir not accessable");
            return;
        }
        dirent *dir;

        int str_index = 0;
        int str_count = 0;
        int file_count = 0;
        while ((dir=readdir(dp))!=0) {
            if (!dir->d_ino) {
                GUI_Log("d_ino?");
                continue;
            }
            switch (dir->d_type) {
                case DT_DIR:
                    //GUI_Log( "[%s]\n", dir->d_name );
                    if( dir->d_name[0] != '.' ) {
                        if( str_index + strlen(dir->d_name) < max_dirs-1 ) {
                            dirList[str_count++] = &dirs[str_index];
                            strcpy( &dirs[str_index], dir->d_name );
                            str_index += strlen(dir->d_name)+1;
                        }
                    }
                    break;
                case DT_REG:
                    GUI_Log( "%s\n", dir->d_name );
                    if( dir->d_name[0] != '.' ) {
                        if( str_index + strlen(dir->d_name) < max_dirs-1 ) {
                            if( fileExtension == NULL || (strlen( dir->d_name ) >= strlen( fileExtension) ) ) {
                                if( fileExtension == NULL || !strcmp( dir->d_name +strlen( dir->d_name)-strlen( fileExtension ), fileExtension ) ) {
                                    fileList[file_count++] = &dirs[str_index];
                                    strcpy( &dirs[str_index], dir->d_name );
                                    str_index += strlen(dir->d_name)+1;
                                }
                                else {
                                    GUI_Log( "no %s %s\n", dir->d_name, fileExtension );
                                }
                            }
                            else {
                                GUI_Log( "Short %i %i\n", strlen( &dirs[str_index]), strlen( fileExtension) );
                            }
                        }
                    }
                    break;
                case DT_LNK:
                    GUI_Log( "%s\n", dir->d_name );
                    break;
            }
            if( str_count >= max_dir-1 )
                break;
            if( file_count >= max_dir-1 )
                break;
        }
        dirList[str_count] = NULL;
        fileList[file_count] = NULL;
        closedir( dp );
        
        int by = 38;
        if( bSaveDlg ) {
            by = 38;
        }
        
        if( dirListBox ) {
            delete dirListBox;
        }
        dirListBox = new GUI_List( this, (const char **)dirList, str_count, -1, by-1, 180+2, this->tw_area.h-titleBarSize+1-by, 0, false,
            [](GUI_List *l,const char *sel, int index) {
                //GUI_Log( "Selected: %s %i %i\n", sel, index, l->selectedIndex);

                if( index == l->selectedIndex ) {
                    GUI_FileDialog *dlg = (GUI_FileDialog *)l->parent->parent;
                    getcwd(dlg->cur_wdir,512);
                    strcat( dlg->cur_wdir, "/" );
                    strcat( dlg->cur_wdir, sel );
                    chdir( dlg->cur_wdir );
                    dlg->bNeedUpdate = true;

                }
            }
        );
        dirListBox->title_str = "Dir List Box";
        
        if( fileListBox ) {
            delete fileListBox;
        }
        fileListBox = new GUI_List( this, (const char **)fileList, file_count, 180, by-1, tw_area.w-180-1, this->tw_area.h-titleBarSize+1-by, 0, false,
                                  [](GUI_List *l,const char *sel, int index) {
                                      if( index == l->selectedIndex ) {
                                          GUI_FileDialog *dlg = (GUI_FileDialog *)l->parent->parent;
                                          //getcwd(dlg->cur_wdir,512);
                                          //strcat( dlg->cur_wdir, "/" );
                                          //strcat( dlg->cur_wdir, sel );
                                          //GUI_Log( "File: %s\n", dlg->cur_wdir );
                                          if( dlg->fileEdit ) {
                                              dlg->fileEdit->setText( sel );
                                          }
                                          
                                      }
                                  }
                                  );
        fileListBox->title_str = "File List Box";
        
        int fc = 0;
        char cwd[512];
        strcpy( cwd, cur_wdir );
        for( long i=strlen( cwd )-1; i>=0; i-- ) {
            if( cwd[i] == '/' ) {
                dirName[fc++] = &cwd[i+1];
                cwd[i] = 0;
                if( fc >= max_dir-1 )
                    break;
            }
        }
        dirName[fc] = NULL;
        if( dirButton ) {
            delete dirButton;
        }
        
        dirButton = new GUI_Button( titleBar, dirName[0], 10, 0, 180, 0, cClear,
                                               [](GUI_Button *bt) {
                                                   GUI_FileDialog *dlg = (GUI_FileDialog *)bt->parent->parent;
                                                   if( dlg->menuDir->isVisible() )
                                                       dlg->menuDir->hide();
                                                   else
                                                       dlg->menuDir->show();
                                               }
                                               );
        dirButton->title_area.x = 10;
        
        
        menuDir = new GUI_List( this, (const char **)&dirName[1], fc-1,
                               10, 1, 160, 0, 0, true,
                               [](GUI_List *l,const char *sel, int index) {
                                   //GUI_Log( "Selected: %s %i\n", sel, index);
                                   
                                   GUI_FileDialog *dlg = (GUI_FileDialog *)l->parent->parent;
                                   //getcwd(dlg->cur_wdir,512);
                                   int fc = 0;
                                   for( long i=strlen( dlg->cur_wdir )-1; i>=0; i-- ) {
                                       if( dlg->cur_wdir[i] == '/' ) {
                                           dlg->cur_wdir[i] = 0;
                                           fc++;
                                           if( fc >= index+1 )
                                               break;
                                       }
                                   }
                                   //GUI_Log( "Dir: %s\n", dlg->cur_wdir );
                                   chdir( dlg->cur_wdir );
                                   dlg->bNeedUpdate = true;
                               }
                               );
        menuDir->canClose = false;
        menuDir->title_str = "Menu Dir";
        menuDir->hide();
        


    }
    GUI_Dialog::draw();
}
Ejemplo n.º 9
0
int
main(int argc, char *argv[])
{
    /* initialize SDL */
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        GUI_Log("Could not initialize SDL");
    }
    if (TTF_Init() != 0){
        GUI_Log( "TTF_Init failed.");
        SDL_Quit();
        return 1;
    }
    if( GUI_Init() != 0 ) {
        GUI_Log( "GUI_Init failed." );
        TTF_Quit();
        SDL_Quit();
        return 1;
    }
    
    char *pref = SDL_GetPrefPath( "JimmySoftware.com", "ToolBarDialog" );
    strcpy( preferenctPath, pref );
    strcat( preferenctPath, "preference.conf" );
    
    SDL_Log( "Pref: %s\n", preferenctPath );
    if( access( preferenctPath, 0 ) != -1 ) {
        GUI_Log( "File Existed\n" );
        loadPalette( preferenctPath );
    } else {
        GUI_Log( "File not existed\n" );
        for( int i=0; i<numPalette; i++ ) {
            for( int j=0; j<numColor; j++ ) {
                mainPalette[i][j] = mColor[j];
            }
        }
    }
    activePalette = 0;
    
    //int sx = 720, sy = 405;
#if __IPHONEOS__ or __ANDROID__
    int sx = 640, sy = 360;
#else
    int sx = 640, sy = 360;
#endif
    SDL_Window *window = GUI_CreateWindow( "GUI", sx, sy );
    if (window == NULL) {
        fatalError("GUI_CreateWindow Error");
        return 1;
    }
    
    SCREEN_WIDTH = GUI_windowWidth;
    SCREEN_HEIGHT = GUI_windowHeight;
    
    topWin=new GUI_TopWin( "TopWin", GUI_Rect(0,0,GUI_windowWidth,GUI_windowHeight),0,
       []( GUI_WinBase *tw ) {
           //tw->clear();
       }
    );
    
    textureFileIcon = loadTexture( "icon_file.png", GUI_renderer );

    
    GUI_WinBase *topBar = new GUI_WinBase( topWin, "TopBar", 0, 0, GUI_windowWidth, 48, cDarkGrey );
    if( topBar ) {
        GUI_Button *bttnFile = new GUI_Button( topBar, "F", 8, 8, 32, 32, cGrey,
            [](GUI_Button *bt) {
                if( menuFile == NULL ) {
                    GUI_Log( "create new MenuFile\n" );
                    static const char *menuText[] = {
                        "File", "Open", "Save", "Exit", NULL
                    };
                    menuFile = new GUI_List( topWin, menuText, 4, bt->tw_area.x, bt->tw_area.y+bt->tw_area.w+10, 240, 0, 0, true,
                        [](GUI_List *l,const char *sel, int index) {
                            switch (index) {
                                case 1:
                                    OpenFile();
                                    break;
                                case 3:
                                    SDL_AddTimer( 100, ExitCallback, NULL );
                                    break;
                                default:
                                    break;
                            }
                            menuFile = NULL;
                        }
                    );
                    menuFile->title_str = "Menu File";
                    menuFile->onClose = [] (GUI_WinBase *) -> bool {
                        menuFile = NULL;
                        return true;
                    };
                }
            }
        );
        bttnFile->texImage = textureFileIcon;
        bttnFile->color = cClear;
        
        new GUI_Button( topBar, "C", 48, 8, 32, 32, cGrey,
            [](GUI_Button *bt) {
                OpenColor();
            }
        );
        
        GUI_WinBase *colorBox = new GUI_WinBase( topBar, "สี", 88, 6, 32*9+18, 36, cClear,
                                                [](GUI_WinBase *wb) {
                                                    GUI_FillRoundRect( 0, 0, wb->tw_area.w, wb->tw_area.h, wb->tw_area.h/2, sdl_color( 0x484848ff ) );
                                                }
                                                );
        if( colorBox ) {
            for( int i=0; i<numColor; i++ ) {
                c[i] = new GUI_Button( colorBox, "", 6+32*i, 3, 31, 31, mainPalette[activePalette][i], setActiveColor );
                c[i]->tag = i;
                c[i]->radius = 15;
                c[i]->border = 1;
            }
            
            setActiveColor( c[0] );
            
            GUI_WinBase *paletteButton = new GUI_WinBase( colorBox, "Palette", 6+32*numColor+5, 3+8, 25, 15, cClear,
                [](GUI_WinBase *w)
                {
                    //GUI_DrawRect( GUI_MakeRect(0, 0, w->tw_area.w, w->tw_area.h), cBlack );
                    SDL_RenderCopy(GUI_renderer, GUI_dropdownTexture, NULL, GUI_MakeRect(1, 1, w->tw_area.w-2, w->tw_area.h-2));
                }
            );
            paletteButton->handle_event_cmd = [](GUI_WinBase *w, SDL_Event* ev ) -> bool {
                switch (ev->type) {
                    case SDL_MOUSEBUTTONUP: {
                        static const char *menuPalette[] = {
                            "Palette1", "Palette2", "Palette3", "Palette4", "Palette5", "---", "Load", "Save", NULL
                        };

                        //GUI_Log( "Palette\n" );
                        GUI_List *listPal = new GUI_List( topWin, menuPalette, 8, w->tw_area.x, w->tw_area.y+w->tw_area.h+18, 240, 0, 0, true,
                                     [](GUI_List* l,const char*sz,int n) {
                                         if( n < numPalette ) {
                                             activePalette = n;
                                             l->setCheck( n );
                                             for( int i=0; i<numColor; i++ ) {
                                                 c[i]->color = mainPalette[n][i];
                                             }
                                         }
                                         else if( n == numPalette + 1 ) {
                                             SDL_AddTimer( 100, LoadPaletteCallback, NULL );
                                         }
                                         else if( n == numPalette + 2 ) {
                                             SDL_AddTimer( 100, SavePaletteCallback, NULL );
                                         }
                                     });
                        for( int i=0; i<numPalette; i++ ) {
                            GUI_ListCell *cell = listPal->getCell( i );
                            cell->checkable = true;
                            cell->display_cmd = cellDraw;
                        }
                        listPal->setCheck( activePalette );
                    }
                }
                return false;
            };

        }
    }
    
    GUI_WinBase *sizeBox = new GUI_WinBase( topBar, "สี", 402, 6, 32*3+6, 36, cClear,
                                            [](GUI_WinBase *wb) {
                                                GUI_FillRoundRect( 0, 0, wb->tw_area.w, wb->tw_area.h, 10, sdl_color( 0x484848ff ) );
                                            }
                                            );
    if( sizeBox ) {
        for( int i=0; i<numSize; i++ ) {
            s[i] = new GUI_Button( sizeBox, "", 3+32*i, 3, 30, 30, cClear, setActiveSize );
            s[i]->tag = i;
            s[i]->radius = 5;
            s[i]->border = 1;
            s[i]->display_cmd = [](GUI_WinBase *w) {
                int bw = 12 + w->tag * 6;
                int cw = 0;
                if( w->tag == 1 )
                    cw -= 3;
                GUI_FillRect( cw+(w->tw_area.w-bw)/2, (w->tw_area.h-bw)/2, bw, bw, cGrey );
                GUI_DrawRect( cw+(w->tw_area.w-bw)/2, (w->tw_area.h-bw)/2, bw, bw, ((GUI_Button*)w)->borderColor );
            };
        }
        setActiveSize( s[0] );
    }
    
    GUI_WinBase *bottomBar = new GUI_WinBase( topWin, "BottomBar", 0, GUI_windowHeight-48, GUI_windowWidth, 48, cDarkGrey );


    //createTestWindows( 20, 20, "Red", cRed );

    
    GUI_Run();
    
    savePalette( preferenctPath );
    
    
    
    /* shutdown SDL */
    GUI_Quit();
    TTF_Quit();
    SDL_Quit();
    
    return 0;
}