add files

This commit is contained in:
2024-10-19 21:55:44 +02:00
parent df60a9a415
commit d932111bae
16 changed files with 5936 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
#include "confirm-dialog.h"
ConfirmDialog::ConfirmDialog()
: c_Label1("_First name", true),
c_ButtonBox(Gtk::Orientation::HORIZONTAL, 5), c_Button_OK("_OK", true),
c_Button_Cancel("_Cancel", true) {
set_destroy_with_parent(true);
set_title("Name Dialog");
set_child(c_Grid);
c_Grid.set_row_spacing(4);
c_Grid.set_column_spacing(4);
c_Grid.set_expand(true);
c_Image.set_from_icon_name("dialog-question");
c_Image.set_icon_size(Gtk::IconSize::LARGE);
c_Grid.attach(c_Image, 0, 0, 1, 2);
c_Grid.attach(c_Label1, 1, 0);
c_Grid.attach(c_Entry1, 2, 0);
c_Label1.set_mnemonic_widget(c_Entry1);
c_Grid.attach(c_ButtonBox, 0, 2, 3, 1);
c_ButtonBox.set_halign(Gtk::Align::END);
c_ButtonBox.append(c_Button_OK);
c_ButtonBox.append(c_Button_Cancel);
}
void ConfirmDialog::buttons_clicked_connect(
const sigc::slot<void(const Glib::ustring &)> &slot) {
c_Button_OK.signal_clicked().connect(sigc::bind(slot, "OK"));
c_Button_Cancel.signal_clicked().connect(sigc::bind(slot, "Cancel"));
}
Glib::ustring ConfirmDialog::get_entry1() const { return c_Entry1.get_text(); }
ConfirmDialog::~ConfirmDialog() {}
+26
View File
@@ -0,0 +1,26 @@
#ifndef GTKMM_NAME_DIALOG_H_
#define GTKMM_NAME_DIALOG_H_
#include <gtkmm.h>
class ConfirmDialog : public Gtk::Window {
public:
ConfirmDialog();
~ConfirmDialog() override;
void
buttons_clicked_connect(const sigc::slot<void(const Glib::ustring &)> &slot);
Glib::ustring get_entry1() const;
protected:
// Member widgets:
Gtk::Grid c_Grid;
Gtk::Image c_Image;
Gtk::Label c_Label1;
Gtk::Entry c_Entry1;
Gtk::Box c_ButtonBox;
Gtk::Button c_Button_OK;
Gtk::Button c_Button_Cancel;
};
#endif /* GTKMM_NAME_DIALOG_H_ */
+174
View File
@@ -0,0 +1,174 @@
#include "main-window.h"
#include "glib.h"
#include "glibmm/ustring.h"
#include "gtk/gtk.h"
#include "gtkmm/image.h"
#include "ultra-mega-functions.h"
#include <bits/stdc++.h>
#include <cstddef>
#include <glib/gi18n.h>
#include <iostream>
#include <ostream>
#include <string>
#include <unistd.h>
MainWindow::MainWindow()
: m_button_spin("spin"), m_button_reset("reset money"),
m_button_quit("Quit"), slot_image0(), slot_image1(), slot_image2(),
m_label_money(), m_label_spins(), m_label_info(), m_label_bet("bet:"),
m_entry_bet(), m_timer_number(0) {
set_title("Pupes Slots!");
m_grid.set_margin(12);
set_child(m_grid);
can_spin = true;
money = 100;
spins = 0;
m_label_spins.set_label(std::to_string(spins));
m_label_money.set_label(std::to_string(money));
// set image size
slot_image0.set_size_request(128, 128);
slot_image1.set_size_request(128, 128);
slot_image2.set_size_request(128, 128);
// status rows
m_grid.attach(m_label_spins, 0, 0);
m_grid.attach(m_label_money, 0, 1);
// row 2: slots
m_grid.attach(slot_image0, 0, 2);
m_grid.attach(slot_image1, 1, 2);
m_grid.attach(slot_image2, 2, 2);
m_grid.attach(m_button_spin, 0, 3);
m_grid.attach(m_label_bet, 1, 3);
m_grid.attach(m_entry_bet, 2, 3);
m_grid.attach(m_button_reset, 1, 4);
m_grid.attach_next_to(m_button_quit, m_button_spin, Gtk::PositionType::BOTTOM,
2, 1);
m_button_spin.signal_clicked().connect(
sigc::bind(sigc::mem_fun(*this, &MainWindow::on_button_spin)));
m_button_quit.signal_clicked().connect(
sigc::mem_fun(*this, &MainWindow::on_button_quit));
}
MainWindow::~MainWindow() {}
void MainWindow::on_button_quit() { set_visible(false); }
// define symbols for slots
Glib::ustring symbol[8]{
IMG_FOLDER "/Arch.svg", IMG_FOLDER "/Debian.svg",
IMG_FOLDER "/Slackware.svg",
IMG_FOLDER "/Beastie.svg", IMG_FOLDER "/DragonFly.svg",
IMG_FOLDER "/Puffy.svg",
IMG_FOLDER "/GNU.svg", IMG_FOLDER "/Tux.svg"};
void MainWindow::randomise_slots(bool save, Glib::ustring *slot_status) {
uint8_t randoms[3];
for (int i = 0; i < (sizeof(randoms) / sizeof(*randoms)); i++) {
randoms[i] = get_random_num(0, ((sizeof(symbol) / sizeof(*symbol)) - 1));
}
slot_image0.set(symbol[randoms[0]]);
slot_image1.set(symbol[randoms[1]]);
slot_image2.set(symbol[randoms[2]]);
if (save) {
for (int i = 0; i < (sizeof(randoms) / sizeof(*randoms)); i++) {
slot_status[i] = symbol[randoms[i]];
}
}
}
bool MainWindow::refresh_slots(int timer_number) {
std::cout << "This is timer " << timer_number;
// decrement and check counter value
if (--m_counters[timer_number] == 0) {
std::cout << " being disconnected" << std::endl;
// delete the counter entry in the STL MAP
m_counters.erase(timer_number);
// delete the connection entry in the STL MAP
m_timers.erase(timer_number);
Glib::ustring slot_status[3];
randomise_slots(true, slot_status);
// debug statement
for (int i = 0; i < (sizeof(slot_status) / sizeof(*slot_status)); i++) {
std::cout << "slot: " << i << " is " << slot_status[i] << std::endl;
}
// check if win
{
if ((slot_status[0].compare(slot_status[1])) == 0 &&
slot_status[0].compare(slot_status[2]) == 0) {
}
}
can_spin = true;
// return false, to stop timer
return false;
} else {
if (m_counters[timer_number] < (count_value * 0.4)) {
if (get_random_num(0, 1) == 1) {
randomise_slots(false, NULL);
}
} else if (m_counters[timer_number] < (count_value * 0.2)) {
if (get_random_num(0, 4) == 1) {
randomise_slots(false, NULL);
}
} else {
randomise_slots(false, NULL);
}
}
// Print the timer value
std::cout << " - " << m_counters[timer_number] << "/" << count_value
<< std::endl;
// Keep going (do not disconnect yet):
return true;
}
void MainWindow::on_button_spin() {
if (can_spin) {
can_spin = false;
spins++;
m_label_spins.set_label(std::to_string(spins));
count_value = get_random_num(30, 50);
// Creation of a new object prevents long lines and shows us a little
// how slots work. We have 0 parameters and bool as a return value
// after calling sigc::bind.
sigc::slot<bool()> my_slot = sigc::bind(
sigc::mem_fun(*this, &MainWindow::refresh_slots), m_timer_number);
// This is where we connect the slot to the Glib::signal_timeout()
auto conn = Glib::signal_timeout().connect(my_slot, 300);
// Remember the connection:
m_timers[m_timer_number] = conn;
// Initialize timer count:
m_counters[m_timer_number] = count_value + 1;
// Print some info to the console for the user:
std::cout << "added timeout " << m_timer_number++ << std::endl;
} else {
std::cerr << "you are already spining\n";
}
}
+52
View File
@@ -0,0 +1,52 @@
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include "confirm-dialog.h"
#include "glibmm/ustring.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
#include <gtkmm.h>
#include <locale>
#define IMG_FOLDER "img"
class MainWindow : public Gtk::Window {
public:
MainWindow();
virtual ~MainWindow();
private:
// Signal handlers:
void on_button_quit();
void on_button_spin();
bool refresh_slots(int timer_number);
void randomise_slots(bool save_to_global, Glib::ustring *slot_status);
// Child widgets:
Gtk::Grid m_grid;
Gtk::Label m_label_money, m_label_spins, m_label_info, m_label_bet;
Gtk::Image slot_image0, slot_image1, slot_image2;
Gtk::Button m_button_spin, m_button_reset, m_button_quit;
Gtk::Entry m_entry_bet;
// money and spin counters that can contain even most dihard gamblers
unsigned long long spins;
unsigned long long money;
// counter boobalob
int m_timer_number;
// These two constants are initialized in the constructor's member
// initializer:
int count_value;
bool can_spin;
// STL map for storing our connections
std::map<int, sigc::connection> m_timers;
// STL map for storing our timer values.
// Each timer counts back from COUNT_VALUE to 0 and is removed when it reaches
// 0
std::map<int, int> m_counters;
};
#endif /* GTKMM_EXAMPLEWINDOW_H */
+9
View File
@@ -0,0 +1,9 @@
#include "main-window.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[]) {
auto app = Gtk::Application::create("org.pupes.gamba");
// Shows the window and returns when it is closed.
return app->make_window_and_run<MainWindow>(argc, argv);
}
+11
View File
@@ -0,0 +1,11 @@
#include "ultra-mega-functions.h"
#include <random>
int get_random_num(int min, int max) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(
min, max); // set range
return dist(rng);
}
+1
View File
@@ -0,0 +1 @@
int get_random_num(int min, int max);