ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/frontend/client/widgets/inventory_widget.cpp
Revision: 1.2
Committed: Mon Oct 18 06:54:04 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.1: +7 -4 lines
Log Message:
gcc 4.5; moved some code; background colour support

File Contents

# User Rev Content
1 sf-pippijn 1.1 #include "dclient/widgets/inventory.h"
2     #include "dclient/render_glyph.h"
3    
4     #include "deliantra/data/constants.h"
5     #include "deliantra/data/glyph.h"
6     #include "deliantra/data/item.h"
7 sf-pippijn 1.2 #include "deliantra/data/inventory.h"
8 sf-pippijn 1.1
9     #include "adt/slice.h"
10    
11     #include <ndk++/pen.hh>
12    
13     #include <algorithm>
14     #include <cassert>
15     #include <cstring>
16    
17     static char const indices[] = " 123456789abcdefghijklmnopqrstuvwxyz";
18     static __attribute__ ((__const__)) int maxidx () { return sizeof indices - 2; }
19    
20     inventory_widget::inventory_widget (ndk::panel *parent)
21     : frame ("inventory", parent)
22     , inv (nullptr)
23     , at (0)
24     {
25     events ()->on_keyboard (slot (&inventory_widget::at_keyboard, this));
26     }
27    
28     int __attribute__ ((__pure__))
29     inventory_widget::maxinv () const
30     {
31     return std::min (height () - 2, maxidx ());
32     }
33    
34     void
35 sf-pippijn 1.2 inventory_widget::update (data::inventory const &inv, std::string const &title)
36 sf-pippijn 1.1 {
37 sf-pippijn 1.2 this->inv = &inv;
38 sf-pippijn 1.1 set_title (title);
39    
40 sf-pippijn 1.2 int const inv_size = this->inv->size ();
41 sf-pippijn 1.1 if (at > inv_size - 2)
42     at = std::max (inv_size - 2, 0);
43 sf-pippijn 1.2
44     visualize ();
45 sf-pippijn 1.1 }
46    
47     void
48     inventory_widget::draw ()
49     {
50     frame::draw ();
51    
52     if (!inv || inv->empty ())
53     return;
54    
55     ndk::pen pen (*this);
56    
57     int i = 0;
58 sf-pippijn 1.2 foreach (data::item const &item, slice (*inv, at))
59 sf-pippijn 1.1 {
60     ++i;
61     pen.move (1, i);
62    
63     // previous page
64     if (at > 0 && i == 1)
65     {
66     pen.write ("%c: more…", indices[i]);
67     continue;
68     }
69    
70     // next page
71     if (i >= maxinv ())
72     {
73     pen.write ("%c: more…", indices[i]);
74     break;
75     }
76    
77     pen.write ("%c: [", indices[i]);
78     write (pen, data::glyphs[item.face]);
79     pen.write ("] ");
80    
81     if (item.nrof < 2)
82     pen.write (item.singular);
83     else
84     pen.write ("%d %s", item.nrof, item.plural.c_str ());
85    
86     if (item.flags & F_OPEN) pen.write (" (open)");
87     if (item.flags & F_APPLIED) pen.write (" (applied)");
88     if (item.flags & F_UNPAID) pen.write (" (unpaid)");
89     if (item.flags & F_MAGIC) pen.write (" (magic)");
90     if (item.flags & F_CURSED) pen.write (" (cursed)");
91     if (item.flags & F_DAMNED) pen.write (" (damned)");
92     if (item.flags & F_LOCKED) pen.write (" *");
93     }
94     }
95    
96     ndk::event::result
97     inventory_widget::at_keyboard (ndk::keyboard const &ev)
98     {
99     if (!inv)
100     return ndk::event::ignored;
101    
102     int const inv_size = inv->size ();
103    
104     switch (ev.code ())
105     {
106     case ndk::key::up:
107     if (at > 0)
108     --at;
109     break;
110    
111     case ndk::key::down:
112     if (at < inv_size - 2)
113     ++at;
114     break;
115    
116     case ndk::key::pg_up:
117     up: at -= std::min (maxinv () - 2, int (at));
118     break;
119    
120     case ndk::key::pg_dn:
121     down: at += std::min (maxinv () - 2, inv_size - at - 2);
122     break;
123    
124     default:
125     if (char const *const iptr = std::strchr (indices, int (ev.code ())))
126     {
127     int const index = iptr - indices;
128     if (index > maxinv () || at + index > inv_size)
129     return ndk::event::ignored;
130    
131     if (index == 1)
132     goto up;
133    
134     if (index == maxinv ())
135     goto down;
136     }
137     return ndk::event::ignored;
138     }
139    
140     draw ();
141    
142     return ndk::event::accepted;
143     }