Skip to content

Commit

Permalink
Add guess game
Browse files Browse the repository at this point in the history
  • Loading branch information
daleclack committed Jun 30, 2021
1 parent 8787af7 commit 24d2f4c
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 15 deletions.
7 changes: 4 additions & 3 deletions Gtkmm3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ pkg_check_modules (GTKMM3 REQUIRED gtkmm-3.0)
include_directories (${GTKMM3_INCLUDE_DIRS})
link_directories (${GTKMM3_LIBRARY_DIRS})

#For win32 platform,use rc resource and .ico icon
if(WIN32)
SET(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
set(app_WINRC icon.rc)
set_property(SOURCE icon.rc APPEND PROPERTY
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/icon.ico
)
add_executable(My_GtkUI WIN32 src/main.cc src/MyWin.cc src/MyStack.cc
src/TopPanel.cc src/LeftPanel.cc src/winconf.cc src/resources.cc ${app_WINRC})
add_executable(My_GtkUI WIN32 ${app_WINRC} src/main.cc src/MyWin.cc src/MyStack.cc
src/Game.cc src/TopPanel.cc src/LeftPanel.cc src/winconf.cc src/resources.cc)
else()
add_executable(My_GtkUI src/main.cc src/MyWin.cc src/MyStack.cc
add_executable(My_GtkUI src/main.cc src/MyWin.cc src/MyStack.cc src/Game.cc
src/TopPanel.cc src/LeftPanel.cc src/winconf.cc src/resources.cc)

endif(WIN32)
Expand Down
1 change: 1 addition & 0 deletions Gtkmm3/README_Gtkmm3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

44 changes: 43 additions & 1 deletion Gtkmm3/res/game1.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">window-close</property>
</object>
<object class="GtkImage" id="image2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">window-minimize</property>
</object>
<object class="GtkWindow" id="window">
<property name="can-focus">False</property>
<property name="border-width">15</property>
<property name="title" translatable="yes">gtk31-guess</property>
<property name="window-position">center</property>
<property name="destroy-with-parent">True</property>
<child>
<!-- n-columns=5 n-rows=7 -->
Expand Down Expand Up @@ -33,6 +43,7 @@
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="left-attach">1</property>
Expand All @@ -48,6 +59,7 @@
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="left-attach">3</property>
Expand All @@ -63,6 +75,7 @@
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="left-attach">1</property>
Expand All @@ -78,6 +91,7 @@
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image-position">top</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="left-attach">3</property>
Expand Down Expand Up @@ -215,5 +229,33 @@
</child>
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="title" translatable="yes">Guess Game</property>
<child>
<object class="GtkButton" id="win_close">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image1</property>
<property name="relief">none</property>
</object>
</child>
<child>
<object class="GtkButton" id="win_mini">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image2</property>
<property name="relief">none</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
Binary file added Gtkmm3/res/icons/16x16/actions/game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gtkmm3/res/icons/16x16/actions/window-close.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gtkmm3/res/icons/16x16/actions/window-minimize.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gtkmm3/res/icons/48x48/actions/game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gtkmm3/res/icons/48x48/actions/game_running.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 28 additions & 1 deletion Gtkmm3/res/leftpanel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
<property name="can-focus">False</property>
<property name="icon-name">gedit</property>
</object>
<object class="GtkImage" id="image7">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="icon-name">game</property>
<property name="icon_size">6</property>
</object>
<object class="GtkImage" id="image9">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="pixel-size">16</property>
<property name="icon-name">game</property>
</object>
<object class="GtkPopover" id="popover1">
<property name="width-request">300</property>
<property name="height-request">400</property>
Expand Down Expand Up @@ -186,7 +198,9 @@
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image9</property>
<property name="relief">none</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="expand">False</property>
Expand Down Expand Up @@ -277,7 +291,20 @@
</packing>
</child>
<child>
<placeholder/>
<object class="GtkButton" id="panel_game">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image7</property>
<property name="relief">none</property>
<property name="image-position">top</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</interface>
6 changes: 6 additions & 0 deletions Gtkmm3/res/my_gtkui.resource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<file>icons/16x16/actions/open-menu.png</file>
<file>icons/16x16/actions/system.png</file>
<file>icons/16x16/actions/vlc.png</file>
<file>icons/16x16/actions/game.png</file>
<file>icons/16x16/actions/window-close.png</file>
<file>icons/16x16/actions/window-minimize.png</file>
<file>icons/48x48/actions/game.png</file>
<file>icons/48x48/actions/game_running.png</file>
<file>icons/48x48/actions/My_GtkUI.png</file>
<file>icons/48x48/actions/user_home.png</file>
<file>icons/48x48/actions/my_user.png</file>
Expand All @@ -19,6 +24,7 @@
<gresource prefix="GtkUI">
<file preprocess="xml-stripblanks">appmenu.xml</file>
<file preprocess="xml-stripblanks">main_stack.ui</file>
<file preprocess="xml-stripblanks">game1.ui</file>
<file preprocess="xml-stripblanks">toppanel.ui</file>
<file preprocess="xml-stripblanks">leftpanel.ui</file>
<file preprocess="xml-stripblanks">win_size.ui</file>
Expand Down
71 changes: 71 additions & 0 deletions Gtkmm3/src/Game.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "Game.hh"
#include <ctime>
#include <cstdlib>
#include <cstdio>

Game::Game()
:running(false),
minimized(false),
game_index{0,1,2,3}
{
//Ininalize Window
game_builder=Gtk::Builder::create_from_resource("/GtkUI/game1.ui");
game_builder->get_widget("window",game_window);
game_builder->get_widget("win_mini",win_mini);
game_builder->get_widget("win_close",win_close);
game_builder->get_widget("btn_exit",btnexit);
game_builder->get_widget("btn_go",btngo);
game_builder->get_widget("label",game_label);
for(int i=0;i<4;i++){
char str[6];
sprintf(str,"btn_%d",i+1);
game_builder->get_widget(str,game_btn[i]);
game_btn[i]->signal_clicked().connect(sigc::bind(
sigc::mem_fun(*this,&Game::gamebtn_clicked),&game_index[i]));
}
//Link Signals
btngo->signal_clicked().connect(sigc::mem_fun(*this,&Game::btngo_clicked));
win_mini->signal_clicked().connect(sigc::mem_fun(*this,&Game::win_minimized));
win_close->signal_clicked().connect(sigc::mem_fun(*this,&Game::win_closed));
btnexit->signal_clicked().connect(sigc::mem_fun(*this,&Game::win_closed));
}

void Game::gamebtn_clicked(int *index){
int flag;
srand((unsigned)time(NULL));
flag=rand()%4;
game_btn[flag]->set_image_from_icon_name("My_GtkUI",Gtk::ICON_SIZE_DIALOG);
if(*index==flag){
game_label->set_label("Good Luck!");
}else{
game_label->set_label("The Button is here!");
}
}

void Game::btngo_clicked(){
for(int i=0;i<4;i++){
game_btn[i]->set_image_from_icon_name("");
}
game_label->set_label("Select a button");
}

void Game::hide_game_window(){
minimized=true;
game_window->hide();
}

void Game::win_minimized(){
minimized=true;
game_window->hide();
}

void Game::win_closed(){
game_window->hide();
running=false;
}

void Game::show_game_window(Gtk::Window &parent){
game_window->set_transient_for(parent);
running=true;
game_window->show_all();
}
27 changes: 27 additions & 0 deletions Gtkmm3/src/Game.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <gtkmm.h>

class Game{
public:
Game();
bool running;
bool minimized;
void show_game_window(Gtk::Window &parent);
void hide_game_window();
private:
//The main builder
Glib::RefPtr<Gtk::Builder> game_builder;
//Child Widgets
Gtk::Window *game_window;
Gtk::Button *win_close,*win_mini;
Gtk::Button *game_btn[4],*btngo,*btnexit;
Gtk::Label *game_label;
int game_index[4];
//Signal Handlers
void gamebtn_clicked(int *index);
void btngo_clicked();
void win_minimized();
void win_closed();
void btnexit_clicked();
};
37 changes: 33 additions & 4 deletions Gtkmm3/src/LeftPanel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <thread>
#include <cstdlib>

LeftPanel::LeftPanel(Gtk::Window *parent1){
LeftPanel::LeftPanel(){
//Get Widgets
panel_builder=Gtk::Builder::create_from_resource("/GtkUI/leftpanel.ui");
panel_builder->get_widget("popover1",popover);
Expand All @@ -12,20 +12,29 @@ LeftPanel::LeftPanel(Gtk::Window *parent1){
panel_builder->get_widget("btnvlc",btnvlc);
panel_builder->get_widget("btn_note",btnnote);
panel_builder->get_widget("btn_vlc",btnvlc_win32);
panel_builder->get_widget("btngame",btngame);
panel_builder->get_widget("panel_game",panelgame);

//Add timer
paneltimer=Glib::signal_timeout().connect(sigc::mem_fun(*this,&LeftPanel::on_timeout),100);

//Link Signals
btnaud->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btnaud_clicked));
btngedit->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btngedit_clicked));
btnvlc->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btnvlc_clicked));
btnnote->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btnnote_clicked));
btnvlc_win32->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::winvlc_clicked));
parent=parent1;
btngame->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btngame_clicked));
panelgame->signal_clicked().connect(sigc::mem_fun(*this,&LeftPanel::btngame_clicked));
}

void LeftPanel::add_panel(Gtk::Overlay &overlay){
void LeftPanel::add_panel(Gtk::Window *parent1,Gtk::Overlay &overlay){
//Pack the panel
btnbox->set_halign(Gtk::ALIGN_START);
btnbox->set_valign(Gtk::ALIGN_START);
overlay.add_overlay(*btnbox);
overlay.add_overlay(*btnbox);
//Set Parent Window
parent=parent1;
}

void LeftPanel::btnaud_clicked(){
Expand Down Expand Up @@ -57,3 +66,23 @@ void LeftPanel::winvlc_clicked(){
fifth.detach();
popover->popdown();
}

void LeftPanel::btngame_clicked(){
if(game1.minimized==true||game1.running==false){
game1.show_game_window(*parent);
game1.minimized=false;
}else{
game1.hide_game_window();
game1.minimized=true;
}
}

bool LeftPanel::on_timeout(){
//When Game running and closed,use different icons
if(game1.running){
panelgame->set_image_from_icon_name("game_running",Gtk::ICON_SIZE_DIALOG);
}else{
panelgame->set_image_from_icon_name("game",Gtk::ICON_SIZE_DIALOG);
}
return true;
}
12 changes: 9 additions & 3 deletions Gtkmm3/src/LeftPanel.hh
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
#pragma once

#include <gtkmm.h>
#include "Game.hh"

class LeftPanel{
public:
LeftPanel(Gtk::Window *parent1);
void add_panel(Gtk::Overlay &overlay);
LeftPanel();
void add_panel(Gtk::Window *parent,Gtk::Overlay &overlay);
private:
//LeftPanel Builder
Glib::RefPtr<Gtk::Builder> panel_builder;
//Child widgets
Gtk::Box *btnbox;
Gtk::Popover *popover;
Gtk::Button *btnaud,*btngedit,*btnvlc,*btnnote,*btnvlc_win32;
Gtk::Button *btnaud,*btngedit,*btnvlc,*btnnote,*btnvlc_win32,*btngame,*panelgame;
//Parent Window
Gtk::Window *parent;
Game game1;
//Timer for panel monitor
sigc::connection paneltimer;
bool on_timeout();
//Signal Handlers for application start
void btnaud_clicked();
void btngedit_clicked();
void btnvlc_clicked();
void btnnote_clicked();
void winvlc_clicked();
void btngame_clicked();
};
Loading

0 comments on commit 24d2f4c

Please sign in to comment.