added basic clipboard functionality; fixed bug in generating random maps

This commit is contained in:
Dave White 2004-06-18 00:12:23 +00:00
parent 7cbf9e80d6
commit 16fcfdc693
7 changed files with 117 additions and 20 deletions

View File

@ -7,6 +7,11 @@
{schedules.cfg}
[+units]
{names.cfg}
[/units]
{multiplayer.cfg}
{terrain.cfg}
@ -121,8 +126,6 @@
{units}
{~units}
{names.cfg}
[race]
name=elf
num_traits=2

44
src/clipboard.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "clipboard.hpp"
#ifdef WIN32
#include <windows.h>
#endif
void copy_to_clipboard(const std::string& text)
{
if(text.empty()) {
return;
}
#ifdef WIN32
if(!OpenClipboard(0)) {
return;
}
EmptyClipboard();
const HGLOBAL clip_buffer = GlobalAlloc(GMEM_DDESHARE,text.size()+1);
char* const buffer = reinterpret_cast<char*>(GlobalLock(clip_buffer));
strcpy(buffer,text.c_str());
GlobalUnlock(clip_buffer);
SetClipboardData(CF_TEXT,clip_buffer);
CloseClipboard();
#endif
}
std::string copy_from_clipboard()
{
#ifdef WIN32
if(OpenClipboard(NULL)) {
const HANDLE data = GetClipboardData(CF_TEXT);
char* const buffer = reinterpret_cast<char*>(GlobalLock(data));
GlobalUnlock(data);
CloseClipboard();
return buffer;
}
#endif
return "";
}

4
src/clipboard.hpp Normal file
View File

@ -0,0 +1,4 @@
#include <string>
void copy_to_clipboard(const std::string& text);
std::string copy_from_clipboard();

View File

@ -1368,12 +1368,13 @@ bool not_id(char c)
void do_interpolation(std::string& res, size_t npos, const string_map* m)
{
std::cerr << "doing interpolation into '" << res << "': " << npos << "\n";
const std::string::iterator i = std::find(res.begin()+npos,res.end(),'$');
if(i == res.end() || i+1 == res.end()) {
return;
}
npos = i - res.begin() + 1;
npos = i - res.begin();
const std::string::iterator end = std::find_if(i+1,res.end(),not_id);
@ -1383,10 +1384,10 @@ void do_interpolation(std::string& res, size_t npos, const string_map* m)
if(m != NULL) {
const string_map::const_iterator itor = m->find(key);
if(itor != m->end()) {
res.insert(npos-1,itor->second);
res.insert(npos,itor->second);
}
} else {
res.insert(npos-1,game_events::get_variable_const(key));
res.insert(npos,game_events::get_variable_const(key));
}
do_interpolation(res,npos,m);

View File

@ -252,6 +252,8 @@ bool generate_river_internal(const height_map& heights, terrain_map& terrain, in
terrain[i->x][i->y] = 'c';
}
std::cerr << "done generating river\n";
return true;
}
@ -533,16 +535,23 @@ std::string generate_name(const unit_race& name_generator, const std::string& id
const std::vector<std::string>& options = config::split(string_table[id]);
if(options.empty() == false) {
const size_t choice = rand()%options.size();
std::cerr << "calling name generator...\n";
const std::string& name = name_generator.generate_name(unit_race::MALE);
std::cerr << "name generator returned '" << name << "'\n";
if(base_name != NULL) {
*base_name = name;
}
std::cerr << "assigned base name..\n";
std::map<std::string,std::string> table;
if(additional_symbols == NULL) {
additional_symbols = &table;
}
std::cerr << "got additional symbols\n";
(*additional_symbols)["name"] = name;
std::cerr << "interpolation variables into '" << options[choice] << "'\n";
return config::interpolate_variables_into_string(options[choice],additional_symbols);
}
@ -670,6 +679,8 @@ std::string default_generate_map(size_t width, size_t height, size_t island_size
naming = *names_info;
}
std::cerr << "random map config: '" << cfg.write() << "'\n";
std::cerr << "making dummy race for naming: '" << naming.write() << "'\n";
//make a dummy race for generating names
unit_race name_generator(naming);
@ -720,7 +731,9 @@ std::string default_generate_map(size_t width, size_t height, size_t island_size
if(river.empty() == false && labels != NULL) {
std::string base_name;
std::cerr << "generating name for river...\n";
const std::string& name = generate_name(name_generator,"river_name",&base_name);
std::cerr << "named river '" << name << "'\n";
size_t name_frequency = 20;
for(std::vector<location>::const_iterator r = river.begin(); r != river.end(); ++r) {
@ -732,8 +745,11 @@ std::string default_generate_map(size_t width, size_t height, size_t island_size
river_names.insert(std::pair<location,std::string>(loc,base_name));
}
std::cerr << "put down river name...\n";
}
std::cerr << "generating lake...\n";
std::set<location> locs;
const bool res = generate_lake(terrain,x,y,atoi(cfg["lake_size"].c_str()),locs);
if(res && labels != NULL) {

View File

@ -28,6 +28,7 @@ markov_prefix_map markov_prefixes(const std::vector<std::string>& items, size_t
std::string markov_generate_name(const markov_prefix_map& prefixes, size_t chain_size, size_t max_len)
{
std::cerr << "generating name with chain size of " << chain_size << " and " << prefixes.size() << " prefixes\n";
if(chain_size == 0)
return "";
@ -35,8 +36,10 @@ std::string markov_generate_name(const markov_prefix_map& prefixes, size_t chain
while(res.size() < max_len) {
const markov_prefix_map::const_iterator i = prefixes.find(prefix);
if(i == prefixes.end() || i->second.empty())
if(i == prefixes.end() || i->second.empty()) {
std::cerr << "failed to find prefix for '" << prefix << "'\n";
return res;
}
const char c = i->second[get_random()%i->second.size()];
if(c == 0) {
@ -104,6 +107,10 @@ unit_race::unit_race(const config& cfg) : name_(cfg["name"]), ntraits_(atoi(cfg[
next_[MALE] = markov_prefixes(names_[MALE],chain_size_);
next_[FEMALE] = markov_prefixes(names_[FEMALE],chain_size_);
if(next_[MALE].size() == 0) {
std::cerr << "empty prefix string: has " << names_[MALE].size() << " names: '" << cfg["male_names"] << "'\n";
}
}
const std::string& unit_race::name() const { return name_; }

View File

@ -11,6 +11,7 @@
See the COPYING file for more details.
*/
#include "textbox.hpp"
#include "../clipboard.hpp"
#include "../font.hpp"
#include "../show_dialog.hpp"
#include "../video.hpp"
@ -20,6 +21,7 @@
#include <algorithm>
#include <cctype>
#include <cstring>
namespace gui {
@ -330,7 +332,21 @@ void textbox::handle_event(const SDL_Event& event)
{
bool changed = false;
if(location().x == 0 || editable_ == false) {
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_c) {
std::cerr << "got press of 'c': " << int(event.key.keysym.mod) << " " << (event.key.keysym.mod&KMOD_CTRL) << " "
<< size_t(selstart_) << "/" << size_t(selend_) << "/" << text_.size() << " " << (focus() ? "focus" : "no focus") << " " << location().x << "\n";
}
//if someone presses ctrl+c to copy text onto the clipboard
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_c && (event.key.keysym.mod&KMOD_CTRL) != 0
&& size_t(selstart_) <= text_.size() && size_t(selend_) <= text_.size() && selstart_ != selend_) {
const size_t beg = minimum<size_t>(size_t(selstart_),size_t(selend_));
const size_t end = maximum<size_t>(size_t(selstart_),size_t(selend_));
std::string selection(end - beg,'x');
std::copy(text_.begin()+beg,text_.begin()+end,selection.begin());
std::cerr << "copying text to clipboard: '" << selection << "'\n";
copy_to_clipboard(selection);
return;
}
@ -340,18 +356,6 @@ void textbox::handle_event(const SDL_Event& event)
grabmouse_ = false;
}
//if we don't have the focus, then see if we gain the focus,
//otherwise return
if(focus() == false) {
if(event.type == SDL_MOUSEMOTION &&
mousex >= location().x && mousey >= location().y &&
mousex < location().x + location().w && mousey < location().y + location().h) {
events::focus_handler(this);
}
return;
}
if( (grabmouse_ && (event.type == SDL_MOUSEMOTION)) || (
event.type == SDL_MOUSEBUTTONDOWN && (mousebuttons & SDL_BUTTON(1)) && !
(mousex < location().x || mousex > location().x + location().w ||
@ -384,8 +388,26 @@ void textbox::handle_event(const SDL_Event& event)
} else if (! (mousebuttons & SDL_BUTTON(1))) {
grabmouse_ = false;
}
set_dirty();
}
if(editable_ == false) {
return;
}
//if we don't have the focus, then see if we gain the focus,
//otherwise return
if(focus() == false) {
if(event.type == SDL_MOUSEMOTION &&
mousex >= location().x && mousey >= location().y &&
mousex < location().x + location().w && mousey < location().y + location().h) {
events::focus_handler(this);
}
return;
}
if(event.type != SDL_KEYDOWN || focus() != true) {
draw();
return;