Skip to content

Commit

Permalink
Handle window resizing using a signal and slot approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyrewulfe committed Mar 14, 2018
1 parent 45c2890 commit 362e265
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/cursesdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define CURSESDEF_H

#include "string_formatter.h"
#include "signals.h"

#include <memory>
#include <cstdint>
Expand Down Expand Up @@ -89,6 +90,7 @@ using chtype = int;
using attr_t = unsigned short;

extern window stdscr;
extern Signal<int, int> window_resized;

window newwin( int nlines, int ncols, int begin_y, int begin_x );
void wborder( const window &win, chtype ls, chtype rs, chtype ts, chtype bs, chtype tl, chtype tr,
Expand All @@ -113,7 +115,7 @@ inline void wprintw( const window &win, const char *const fmt, Args &&... args )
return wprintw( win, string_format( fmt, std::forward<Args>( args )... ) );
}

void resizeterm();
void resizeterm( int w, int h );
void werase( const window &win );
void init_pair( short pair, base_color f, base_color b );
void wmove( const window &win, int y, int x );
Expand Down
7 changes: 4 additions & 3 deletions src/cursesport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

catacurses::window catacurses::stdscr;
std::array<cata_cursesport::pairs, 100> cata_cursesport::colorpairs; //storage for pair'ed colored
Signal<int, int> catacurses::window_resized;

static bool wmove_internal( const catacurses::window &win_, const int y, const int x )
{
Expand Down Expand Up @@ -408,10 +409,10 @@ void catacurses::mvwprintw(const window &win, int y, int x, const std::string &p
return printstring(win.get<cata_cursesport::WINDOW>(), printbuf);
}

//Resizes the underlying terminal after a Window's console resize(maybe?) Not used in TILES
void catacurses::resizeterm()
//Resizes the underlying terminal after a Window's console resize. Not used in TILES
void catacurses::resizeterm( int w, int h )
{
g->init_ui();
catacurses::window_resized.emit( w, h );
}

//erases a window of all text and attributes
Expand Down
6 changes: 6 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ void game::reenter_fullscreen()
}
}

// Callback for main window resize signal
void game::on_window_resized( int, int )
{
init_ui();
}

/*
* Initialize more stuff after mapbuffer is loaded.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ class game
void toggle_pixel_minimap(void);
void temp_exit_fullscreen(void);
void reenter_fullscreen(void);
void on_window_resized( int w = 0, int h = 0 ); // handles main window resize
void zoom_in();
void zoom_out();
void reset_zoom();
Expand Down Expand Up @@ -981,7 +982,6 @@ class game
void draw_sidebar();
void draw_sidebar_messages();
void draw_pixel_minimap(); // Draws the pixel minimap based on the player's current location

// int autosave_timeout(); // If autosave enabled, how long we should wait for user inaction before saving.
void autosave(); // automatic quicksaves - Performs some checks before calling quicksave()
void quicksave(); // Saves the game without quitting
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ int main(int argc, char *argv[])
}

// Now we do the actual game.

g->init_ui();
catacurses::window_resized.connect_member(g, &game::on_window_resized);

catacurses::curs_set( 0 ); // Invisible cursor here, because MAPBUFFER.load() is crash-prone

Expand Down
13 changes: 6 additions & 7 deletions src/ncurses_def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "catacharset.h"
#include "color.h"

#include "game.h"
#include <stdexcept>

extern int VIEW_OFFSET_X; // X position of terrain window
Expand Down Expand Up @@ -197,13 +196,12 @@ void catacurses::init_pair( const short pair, const base_color f, const base_col
}

catacurses::window catacurses::stdscr;
Signal<int, int> catacurses::window_resized;

void catacurses::resizeterm()
void catacurses::resizeterm( int w, int h )
{
const int new_x = ::getmaxx( stdscr.get<::WINDOW>() );
const int new_y = ::getmaxy( stdscr.get<::WINDOW>() );
if( ::is_term_resized( new_x, new_y ) ) {
g->init_ui();
if( ::is_term_resized( w, h ) ) {
window_resized.emit( w, h );
}
}

Expand Down Expand Up @@ -256,7 +254,8 @@ input_event input_manager::get_input_event()
}
// ncurses mouse handling
} else if( key == KEY_RESIZE ) {
catacurses::resizeterm();
catacurses::resizeterm( ::getmaxx( catacurses::stdscr.get<::WINDOW>() ),
::getmaxy( catacurses::stdscr.get<::WINDOW>() ) );
} else if( key == KEY_MOUSE ) {
MEVENT event;
if( getmouse( &event ) == OK ) {
Expand Down
5 changes: 2 additions & 3 deletions src/sdltiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,17 +1252,16 @@ long sdl_keysym_to_curses( SDL_Keysym keysym )
}
}

bool handle_resize(int w, int h)
bool handle_resize( int w, int h )
{
if( ( w != WindowWidth ) || ( h != WindowHeight ) ) {
WindowWidth = w;
WindowHeight = h;
TERMINAL_WIDTH = WindowWidth / fontwidth;
TERMINAL_HEIGHT = WindowHeight / fontheight;
SetupRenderTarget();
g->init_ui();
tilecontext->reinit_minimap();

catacurses::window_resized.emit( TERMINAL_WIDTH, TERMINAL_HEIGHT );
return true;
}
return false;
Expand Down
72 changes: 72 additions & 0 deletions src/signals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

// Template courtesy of Simon Schneegans, see: http://simmesimme.github.io/tutorials/2015/09/20/signal-slot

#ifndef SIGNALS_HPP
#define SIGNALS_HPP

#include <functional>
#include <map>


template <typename... Args>
class Signal
{

public:

Signal() : current_id_( 0 ) {}

// copy creates new Signal
Signal( Signal const & ) : current_id_( 0 ) {}

// connects a member function to this Signal
template <typename T>
int connect_member( T *inst, void ( T::*func )( Args... ) ) {
return connect( [ = ]( Args... args ) {
( inst->*func )( args... );
} );
}

// connects a const member function to this Signal
template <typename T>
int connect_member( T *inst, void ( T::*func )( Args... ) const ) {
return connect( [ = ]( Args... args ) {
( inst->*func )( args... );
} );
}

// connects a std::function to the Signal. The returned
// value can be used to disconnect the function again
int connect( std::function<void( Args... )> const &slot ) const {
slots_.insert( std::make_pair( ++current_id_, slot ) );
return current_id_;
}

// disconnects a previously connected function
void disconnect( int id ) const {
slots_.erase( id );
}

// disconnects all previously connected functions
void disconnect_all() const {
slots_.clear();
}

// calls all connected functions
void emit( Args... p ) {
for( auto it : slots_ ) {
it.second( p... );
}
}

// assignment creates new Signal
Signal &operator=( Signal const & ) {
disconnect_all();
}

private:
mutable std::map<int, std::function<void( Args... )>> slots_;
mutable int current_id_;
};

#endif /* SIGNALS_H */
18 changes: 1 addition & 17 deletions src/wincurse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void handle_resize()
TERMINAL_HEIGHT = WndRect.bottom / fontheight;
WindowWidth = TERMINAL_WIDTH * fontwidth;
WindowHeight = TERMINAL_HEIGHT * fontheight;
catacurses::resizeterm();
catacurses::resizeterm( TERMINAL_WIDTH, TERMINAL_HEIGHT );
create_backbuffer();
SetBkMode(backbuffer, TRANSPARENT);//Transparent font backgrounds
SelectObject(backbuffer, font);//Load our font into the DC
Expand Down Expand Up @@ -310,22 +310,6 @@ LRESULT CALLBACK ProcessMessages(HWND__ *hWnd,unsigned int Msg,
case WM_SIZE:
case WM_SIZING:
needs_resize = true;
RECT WndRect;
if( GetClientRect( WindowHandle, &WndRect ) ) {
TERMINAL_WIDTH = WndRect.right / fontwidth;
TERMINAL_HEIGHT = WndRect.bottom / fontheight;
WindowWidth = TERMINAL_WIDTH * fontwidth;
WindowHeight = TERMINAL_HEIGHT * fontheight;
catacurses::resizeterm();
create_backbuffer();
SetBkMode(backbuffer, TRANSPARENT);//Transparent font backgrounds
SelectObject(backbuffer, font);//Load our font into the DC
color_loader<RGBQUAD>().load( windowsPalette );
if( SetDIBColorTable(backbuffer, 0, windowsPalette.size(), windowsPalette.data() ) == 0 ) {
throw std::runtime_error( "SetDIBColorTable failed" );
}
catacurses::refresh();
}
return 0;

case WM_SYSCHAR:
Expand Down

0 comments on commit 362e265

Please sign in to comment.