summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpauldubois <pauldubois@c06c8d41-db1a-0410-9941-cceddc491573>2008-04-07 06:20:32 +0000
committerpauldubois <pauldubois@c06c8d41-db1a-0410-9941-cceddc491573>2008-04-07 06:20:32 +0000
commit2f63d14e46fcb39b0021fb209f85207cddf7b404 (patch)
tree9bb9d77a3394ff6b159397308aeb2f8651ecb468
parent149c6fb2d95c12bcd27c067929da23c8a8335f33 (diff)
downloadcrawl-ref-2f63d14e46fcb39b0021fb209f85207cddf7b404.tar.gz
crawl-ref-2f63d14e46fcb39b0021fb209f85207cddf7b404.zip
For David. Menu's draw_title_suffix can now accept a formatted_string
instead of a string. See example in StashSearchMenu::draw_title. If you want everything in cyan, you might want to figure out who sets the StashSearchMenu's title->color and change that, too. Added formatted_string::substr(). Lightly tested, seems to work fine. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@4091 c06c8d41-db1a-0410-9941-cceddc491573
-rw-r--r--crawl-ref/source/format.cc56
-rw-r--r--crawl-ref/source/format.h1
-rw-r--r--crawl-ref/source/menu.cc37
-rw-r--r--crawl-ref/source/menu.h3
-rw-r--r--crawl-ref/source/stash.cc4
5 files changed, 99 insertions, 2 deletions
diff --git a/crawl-ref/source/format.cc b/crawl-ref/source/format.cc
index ea97f21b4d..f9cc90a28f 100644
--- a/crawl-ref/source/format.cc
+++ b/crawl-ref/source/format.cc
@@ -406,6 +406,62 @@ int formatted_string::find_last_colour() const
return (LIGHTGREY);
}
+formatted_string formatted_string::substr(size_t start, size_t substr_length) const
+{
+ const unsigned int NONE = (unsigned int)-1;
+ unsigned int last_FSOP_COLOUR = NONE;
+ unsigned int last_FSOP_CURSOR = NONE;
+
+ // Find the first string to copy
+ unsigned int i;
+ for (i=0; i<ops.size(); ++i)
+ {
+ const fs_op& op = ops[i];
+ if (op.type == FSOP_COLOUR)
+ last_FSOP_CURSOR = i;
+ else if (op.type == FSOP_CURSOR)
+ last_FSOP_CURSOR = i;
+ else if (op.type == FSOP_TEXT)
+ if (op.text.length() > start)
+ break;
+ else
+ start -= op.text.length();
+ }
+
+ if (i == ops.size())
+ return formatted_string();
+
+ formatted_string result;
+ // set up the state
+ if (last_FSOP_COLOUR != NONE)
+ result.ops.push_back(ops[last_FSOP_COLOUR]);
+ if (last_FSOP_CURSOR != NONE)
+ result.ops.push_back(ops[last_FSOP_CURSOR]);
+
+ // Copy the text
+ for ( ; i<ops.size(); i++)
+ {
+ const fs_op& op = ops[i];
+ if (op.type == FSOP_TEXT)
+ {
+ result.ops.push_back(op);
+ std::string& new_string = result.ops[result.ops.size()-1].text;
+ if (start > 0 || op.text.length() > substr_length)
+ new_string = new_string.substr(start, substr_length);
+
+ substr_length -= new_string.length();
+ if (substr_length == 0)
+ break;
+ }
+ else
+ {
+ result.ops.push_back(op);
+ }
+ }
+
+ return result;
+}
+
void formatted_string::add_glyph(const item_def *item)
{
const int last_col = find_last_colour();
diff --git a/crawl-ref/source/format.h b/crawl-ref/source/format.h
index 8b17b2a679..e2b997e3b8 100644
--- a/crawl-ref/source/format.h
+++ b/crawl-ref/source/format.h
@@ -40,6 +40,7 @@ public:
void add_glyph(const monsters *mons);
void add_glyph(const item_def *item);
void textcolor(int color);
+ formatted_string substr(size_t index, size_t length=std::string::npos) const;
void clear();
diff --git a/crawl-ref/source/menu.cc b/crawl-ref/source/menu.cc
index 783d3dd69f..40be0a8598 100644
--- a/crawl-ref/source/menu.cc
+++ b/crawl-ref/source/menu.cc
@@ -428,6 +428,43 @@ bool Menu::draw_title_suffix( const std::string &s, bool titlefirst )
return true;
}
+bool Menu::draw_title_suffix( const formatted_string &fs, bool titlefirst )
+{
+ int oldx = wherex(), oldy = wherey();
+
+ if (titlefirst)
+ draw_title();
+
+ int x = wherex();
+ if (x > get_number_of_cols() || x < 1)
+ {
+ cgotoxy(oldx, oldy);
+ return false;
+ }
+
+ // Note: 1 <= x <= get_number_of_cols(); we have no fear of overflow.
+ const unsigned int avail_width = get_number_of_cols() - x + 1;
+ const unsigned int fs_length = fs.length();
+ if (fs_length > avail_width)
+ {
+ formatted_string fs_trunc = fs.substr(0, avail_width);
+ fs_trunc.display();
+ }
+ else
+ {
+ fs.display();
+ if (fs_length < avail_width)
+ {
+ char fmt[20];
+ sprintf(fmt, "%%%ds", avail_width-fs_length);
+ cprintf(fmt, " ");
+ }
+ }
+
+ cgotoxy( oldx, oldy );
+ return true;
+}
+
void Menu::draw_select_count( int count, bool force )
{
if (!force && !is_set(MF_MULTISELECT))
diff --git a/crawl-ref/source/menu.h b/crawl-ref/source/menu.h
index 05c545747e..e1af2f760e 100644
--- a/crawl-ref/source/menu.h
+++ b/crawl-ref/source/menu.h
@@ -19,6 +19,8 @@
#include "defines.h"
#include "libutil.h"
+class formatted_string;
+
enum MenuEntryLevel
{
MEL_NONE = -1,
@@ -218,6 +220,7 @@ public:
void set_tag(const std::string& t) { tag = t; }
bool draw_title_suffix( const std::string &s, bool titlefirst = true );
+ bool draw_title_suffix( const formatted_string &fs, bool titlefirst = true );
void update_title();
// Sets a replacement for the --more-- string.
diff --git a/crawl-ref/source/stash.cc b/crawl-ref/source/stash.cc
index 2a2e47ee0c..66629a968f 100644
--- a/crawl-ref/source/stash.cc
+++ b/crawl-ref/source/stash.cc
@@ -1605,10 +1605,10 @@ void StashSearchMenu::draw_title()
char buf[200];
snprintf(buf, 200,
- " [a-z: %s ?: change action /: change sort]",
+ "<lightgrey> [<w>a-z</w>: %s <w>?</w>: change action <w>/</w>: change sort]",
menu_action == ACT_TRAVEL ? "travel" : "examine");
- draw_title_suffix(buf, false);
+ draw_title_suffix(formatted_string::parse_string(buf), false);
}
}