/* * File: guic-win.cc * Summary: 1) Image manipulation routines * 2) WinClass and RegionClass system independent imprementaions * see guic-*.cc for system dependent implementations * Created by: ennewalker on Sat Jan 5 01:33:53 2008 UTC * * Modified for Crawl Reference by $Author: j-p-e-g $ on $Date: 2008-03-07 $ */ #include "AppHdr.h" #include "debug.h" #include "guic.h" #ifdef WIN32TILES #else #define dos_char false; #endif int TextRegionClass::print_x; int TextRegionClass::print_y; TextRegionClass *TextRegionClass::text_mode = NULL; int TextRegionClass::text_col = 0; TextRegionClass *TextRegionClass::cursor_region= NULL; int TextRegionClass::cursor_flag = 0; int TextRegionClass::cursor_x; int TextRegionClass::cursor_y; // more logical color naming const int map_colors[MAX_MAP_COL][3]= { { 0, 0, 0}, // BLACK {128, 128, 128}, // DKGREY {160, 160, 160}, // MDGREY {192, 192, 192}, // LTGREY {255, 255, 255}, // WHITE { 0, 64, 255}, // BLUE (actually cyan-blue) {128, 128, 255}, // LTBLUE { 0, 32, 128}, // DKBLUE (maybe too dark) { 0, 255, 0}, // GREEN {128, 255, 128}, // LTGREEN { 0, 128, 0}, // DKGREEN { 0, 255, 255}, // CYAN { 64, 255, 255}, // LTCYAN (maybe too pale) { 0, 128, 128}, // DKCYAN {255, 0, 0}, // RED {255, 128, 128}, // LTRED (actually pink) {128, 0, 0}, // DKRED {192, 0, 255}, // MAGENTA (actually blue-magenta) {255, 128, 255}, // LTMAGENTA { 96, 0, 128}, // DKMAGENTA {255, 255, 0}, // YELLOW {255, 255, 64}, // LTYELLOW (maybe too pale) {128, 128, 0}, // DKYELLOW {165, 91, 0}, // BROWN }; const int term_colors[MAX_TERM_COL][3]= { { 0, 0, 0}, // BLACK { 0, 82, 255}, // BLUE {100, 185, 70}, // GREEN { 0, 180, 180}, // CYAN {255, 48, 0}, // RED {238, 92, 238}, // MAGENTA {165, 91, 0}, // BROWN {162, 162, 162}, // LIGHTGREY { 82, 82, 82}, // DARKGREY { 82, 102, 255}, // LIGHTBLUE { 82, 255, 82}, // LIGHTGREEN { 82, 255, 255}, // LIGHTCYAN {255, 82, 82}, // LIGHTRED {255, 82, 255}, // LIGHTMAGENTA {255, 255, 82}, // YELLOW {255, 255, 255} // WHITE }; WinClass::WinClass() { // Minimum; wx = 10; wy = 10; ox = 0; oy = 0; SysInit(); } WinClass::~WinClass() { SysDeinit(); regions.clear(); layers.clear(); } void WinClass::placeRegion(RegionClass *r, int layer0, int x, int y, int margin_top, int margin_left, int margin_bottom, int margin_right) { if (r->win == NULL) { regions.push_back(r); layers.push_back(layer0); } r->win = this; r->layer = layer0; r->flag = true; r->sx = x; r->sy = y; r->ox = r->sx + margin_left; r->oy = r->sy + margin_top; r->wx = r->dx * r->mx + margin_left + margin_right; r->wy = r->dy * r->my + margin_top + margin_bottom; r->ex = r->sx + r->wx; r->ey = r->sy + r->wy; if (r->ex > wx) wx = r->ex; if (r->ey > wy) wy = r->ey; } void WinClass::placeRegion(RegionClass *r, int layer0, RegionClass *neighbor, int pflag, int margin_top, int margin_left, int margin_bottom, int margin_right) { int sx0 =0; int sy0 =0; int ex0 =0; int ey0 =0; int x = 0; int y = 0; if (neighbor!=NULL) { sx0 = neighbor->sx; sy0 = neighbor->sy; ex0 = neighbor->ex; ey0 = neighbor->ey; } if (pflag == PLACE_RIGHT) { x = ex0; y = sy0; } else { x = sx0; y = ey0; } placeRegion(r, layer0, x, y, margin_top, margin_left, margin_bottom, margin_right); } void WinClass::removeRegion(RegionClass *r) { for (unsigned int i = 0; i < regions.size(); i++) { if (regions[i] == r) { for (unsigned int j = i + 1; j < regions.size(); j++) { regions[j-1] = regions[j]; layers[j-1] = layers[j]; } regions.pop_back(); layers.pop_back(); return; } } } void WinClass::redraw(int x1, int y1, int x2, int y2) { std::vector ::iterator r; int cx1, cx2, cy1, cy2; for (r = regions.begin();r != regions.end();r++) { if (!(*r)->is_active()) continue; if( (*r)->convert_redraw_rect(x1, y1, x2, y2, &cx1, &cy1, &cx2, &cy2)) { (*r)->redraw(cx1, cy1, cx2, cy2); } } } void WinClass::redraw() { redraw(0, 0, wx-1, wy-1); } void WinClass::move(int ox0, int oy0) { ox = ox0; oy = oy0; move(); // system dependent } void WinClass::resize(int wx0, int wy0) { if (wx0>0) wx = wx0; if (wy0>0) wy = wy0; resize(); // system dependent } RegionClass::RegionClass() { flag = false; win = NULL; backbuf = NULL; SysInit(); ox = oy = 0; dx = dy = 1; font_copied = false; id = 0; } RegionClass::~RegionClass() { SysDeinit(); if (backbuf != NULL) ImgDestroy(backbuf); } void TextRegionClass::resize(int x, int y) { int i; free(cbuf); free(abuf); cbuf = (unsigned char *)malloc(x*y); abuf = (unsigned char *)malloc(x*y); for (i=0; iactive_layer == layer) return true; else return false; } void RegionClass::make_active() { if (!flag) return; win->active_layer = layer; } void RegionClass::redraw(int x1, int y1, int x2, int y2) { } void RegionClass::redraw() { redraw(0, 0, mx-1, my-1); } void MapRegionClass::redraw() { redraw(0, 0, mx-1, my-1); } void TileRegionClass::redraw() { redraw(0, 0, mx*dx-1, my*dy-1); } void MapRegionClass::set_col(int col, int x, int y) { mbuf[x + y * mx2] = col; } int MapRegionClass::get_col(int x, int y) { return mbuf[x + y * mx2]; } /*------------------------------------------*/ bool RegionClass::convert_redraw_rect(int x1, int y1, int x2, int y2, int *rx1, int *ry1, int *rx2, int *ry2) { int cx1 = x1-ox; int cy1 = y1-oy; int cx2 = x2-ox; int cy2 = y2-oy; if ( (cx2 < 0) || (cy2 < 0) || (cx1 >= dx*mx) || (cy1 >=dy*my)) return false; cx1 /= dx; cy1 /= dy; cx2 /= dx; cy2 /= dy; if(cx2>=mx-1)cx2=mx-1; if(cy2>=my-1)cy2=my-1; if(cx1<0) cx1=0; if(cy1<0) cy1=0; *rx1 = cx1; *ry1 = cy1; *rx2 = cx2; *ry2 = cy2; return true; } bool TileRegionClass::convert_redraw_rect(int x1, int y1, int x2, int y2, int *rx1, int *ry1, int *rx2, int *ry2) { int cx1 = x1-ox; int cy1 = y1-oy; int cx2 = x2-ox; int cy2 = y2-oy; int wwx = dx*mx; int wwy = dy*my; if ( (cx2 < 0) || (cy2 < 0) || (cx1 >= wwx) || (cy1 >=wwy)) return false; if(cx2>=wwx-1)cx2=wwx-1; if(cy2>=wwy-1)cy2=wwy-1; if(cx1<0) cx1=0; if(cy1<0) cy1=0; *rx1 = cx1; *ry1 = cy1; *rx2 = cx2; *ry2 = cy2; return true; } bool RegionClass::mouse_pos(int mouse_x, int mouse_y, int *cx, int *cy) { int x = mouse_x - ox; int y = mouse_y - oy; if (!is_active()) return false; if ( x < 0 || y < 0 ) return false; x /= dx; y /= dy; if (x >= mx || y >= my) return false; *cx = x; *cy = y; return true; } bool MapRegionClass::mouse_pos(int mouse_x, int mouse_y, int *cx, int *cy) { int x = mouse_x - ox; int y = mouse_y - oy; if ( x < 0 || y < 0 ) return false; x /= dx; y /= dy; if (x >= mx || y >= my) return false; if (!is_active()) return false; *cx = x; *cy = y; return true; } bool TileRegionClass::mouse_pos(int mouse_x, int mouse_y, int *cx, int *cy) { int x = mouse_x - ox; int y = mouse_y - oy; if (!is_active()) return false; if ( x < 0 || y < 0 ) return false; if ( x >= dx*mx || y >= dy*my ) return false; x /= dx; y /= dy; *cx = x; *cy = y; return true; } /* * Text related */ void TextRegionClass::scroll() { int idx; if(!flag) return; for(idx=0; idx 0) print_y -= 1; if (cursor_y > 0) cursor_y -= 1; } void TextRegionClass::adjust_region(int *x1, int *x2, int y) { *x2 = *x2 + 1; } void TextRegionClass::addstr(char *buffer) { int i,j; char buf2[1024]; int len = strlen(buffer); if(!flag)return; j=0; for(i=0;ierase_cursor(); cursor_region = NULL; } if (cursor_flag) { text_mode->draw_cursor(print_x, print_y); cursor_x = print_x; cursor_y = print_y; cursor_region = text_mode; } } int TextRegionClass::wherex() { return print_x + 1; } int TextRegionClass::wherey() { return print_y + 1; } void TextRegionClass::_setcursortype(int curstype) { cursor_flag = curstype; if (cursor_region != NULL) cursor_region ->erase_cursor(); if (curstype) { text_mode->draw_cursor(print_x, print_y); cursor_x = print_x; cursor_y = print_y; cursor_region = text_mode; } }