A bit of refactoring:

- common code of ChoiceValueViewer and MultipleChoiceValueViewer put into functions
 - RotatedDC can now draw text with shadow.
 - DECLARE_STYLE_TYPE macro and friends do slightly more.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@788 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2007-12-26 17:46:40 +00:00
parent 2fb7de196e
commit 8d86e4a25c
19 changed files with 125 additions and 233 deletions
+3 -8
View File
@@ -13,19 +13,14 @@
void FontTextElement::draw(RotatedDC& dc, double scale, const RealRect& rect, const double* xs, DrawWhat what, size_t start, size_t end) const {
if ((what & draw_as) != draw_as) return; // don't draw
dc.SetFont(*font, scale);
// draw shadow
// text
String text = content.substr(start - this->start, end - start);
if (!text.empty() && text.GetChar(text.size() - 1) == _('\n')) {
text = text.substr(0, text.size() - 1); // don't draw last \n
}
if (font->hasShadow()) {
dc.SetTextForeground(font->shadow_color);
dc.DrawText(text, rect.position() + font->shadow_displacement);
}
// draw
dc.SetTextForeground(font->color);
dc.DrawText(text, rect.position());
dc.SetFont(*font, scale);
dc.DrawTextWithShadow(text, *font, rect.position());
}
void FontTextElement::getCharInfo(RotatedDC& dc, double scale, vector<CharInfo>& out) const {
+53 -50
View File
@@ -12,27 +12,38 @@
// ----------------------------------------------------------------------------- : ChoiceValueViewer
void get_options(Rotation& rot, DataViewer& viewer, const ChoiceStyle& style, GeneratedImage::Options& opts);
bool ChoiceValueViewer::prepare(RotatedDC& dc) {
if (style().render_style & RENDER_IMAGE) {
style().initImage();
CachedScriptableImage& img = style().image;
return prepare_choice_viewer(dc, viewer, style(), value().value());
}
void ChoiceValueViewer::draw(RotatedDC& dc) {
drawFieldBorder(dc);
if (style().render_style & RENDER_HIDDEN) return;
draw_choice_viewer(dc, viewer, style(), value().value());
}
bool prepare_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value) {
if (style.render_style & RENDER_IMAGE) {
style.initImage();
CachedScriptableImage& img = style.image;
Context& ctx = viewer.getContext();
ctx.setVariable(SCRIPT_VAR_input, to_script(value().value()));
ctx.setVariable(SCRIPT_VAR_input, to_script(value));
// generate to determine the size
if (img.update(ctx) && img.isReady()) {
GeneratedImage::Options img_options;
getOptions(dc, img_options);
get_options(dc, viewer, style, img_options);
// Generate image/bitmap (whichever is available)
// don't worry, we cache the image
ImageCombine combine = style().combine;
style().loadMask(*viewer.stylesheet);
ImageCombine combine = style.combine;
style.loadMask(*viewer.stylesheet);
Bitmap bitmap; Image image;
RealSize size;
img.generateCached(img_options, &style().mask, &combine, &bitmap, &image, &size);
img.generateCached(img_options, &style.mask, &combine, &bitmap, &image, &size);
// store content properties
if (style().content_width != size.width || style().content_height != size.height) {
style().content_width = size.width;
style().content_height = size.height;
if (style.content_width != size.width || style.content_height != size.height) {
style.content_width = size.width;
style.content_height = size.height;
return true;
}
}
@@ -40,25 +51,23 @@ bool ChoiceValueViewer::prepare(RotatedDC& dc) {
return false;
}
void ChoiceValueViewer::draw(RotatedDC& dc) {
drawFieldBorder(dc);
if (style().render_style & RENDER_HIDDEN) return;
if (value().value().empty()) return;
void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value) {
if (value.empty()) return;
double margin = 0;
if (style().render_style & RENDER_IMAGE) {
if (style.render_style & RENDER_IMAGE) {
// draw image
CachedScriptableImage& img = style().image;
CachedScriptableImage& img = style.image;
if (img.isReady()) {
GeneratedImage::Options img_options;
getOptions(dc, img_options);
get_options(dc, viewer, style, img_options);
// Generate image/bitmap
ImageCombine combine = style().combine;
style().loadMask(*viewer.stylesheet);
ImageCombine combine = style.combine;
style.loadMask(*viewer.stylesheet);
Bitmap bitmap; Image image;
RealSize size;
img.generateCached(img_options, &style().mask, &combine, &bitmap, &image, &size);
img.generateCached(img_options, &style.mask, &combine, &bitmap, &image, &size);
size = dc.trInvS(size);
RealRect rect(align_in_rect(style().alignment, size, dc.getInternalRect()), size);
RealRect rect(align_in_rect(style.alignment, size, dc.getInternalRect()), size);
if (bitmap.Ok()) {
// just draw it
dc.DrawPreRotatedBitmap(bitmap,rect);
@@ -67,42 +76,36 @@ void ChoiceValueViewer::draw(RotatedDC& dc) {
dc.DrawPreRotatedImage(image,rect,combine);
}
margin = size.width + 1;
} else if (nativeLook()) {
} else if (viewer.nativeLook()) {
// always have the margin
margin = 17;
}
}
if (style().render_style & RENDER_TEXT) {
// draw text
dc.SetFont(style().font, 1.0);
String text = tr(*viewer.stylesheet, value().value(), capitalize(value().value()));
if (style.render_style & RENDER_TEXT) {
String text = tr(*viewer.stylesheet, value, capitalize(value));
dc.SetFont(style.font, 1.0);
RealPoint pos = align_in_rect(ALIGN_MIDDLE_LEFT, RealSize(0, dc.GetCharHeight()), dc.getInternalRect()) + RealSize(margin, 0);
if (style().font.hasShadow()) {
dc.SetTextForeground(style().font.shadow_color);
dc.DrawText(text, pos + style().font.shadow_displacement);
}
dc.SetTextForeground(style().font.color());
dc.DrawText(text, pos);
dc.DrawTextWithShadow(text, style.font, pos);
}
}
void get_options(Rotation& rot, DataViewer& viewer, const ChoiceStyle& style, GeneratedImage::Options& opts) {
opts.package = viewer.stylesheet.get();
opts.local_package = viewer.getSet().get();
opts.angle = rot.trAngle(0);
if (viewer.nativeLook()) {
opts.width = opts.height = 16;
opts.preserve_aspect = ASPECT_BORDER;
} else if(style.render_style & RENDER_TEXT) {
// also drawing text, use original size
} else {
opts.width = (int) rot.trX(style.width);
opts.height = (int) rot.trY(style.height);
opts.preserve_aspect = (style.alignment & ALIGN_STRETCH) ? ASPECT_STRETCH : ASPECT_FIT;
}
}
void ChoiceValueViewer::onStyleChange(int changes) {
if (changes & CHANGE_MASK) style().image.clearCache();
ValueViewer::onStyleChange(changes);
}
void ChoiceValueViewer::getOptions(Rotation& rot, GeneratedImage::Options& opts) {
opts.package = viewer.stylesheet.get();
opts.local_package = &getSet();
opts.angle = rot.trAngle(0);
if (nativeLook()) {
opts.width = opts.height = 16;
opts.preserve_aspect = ASPECT_BORDER;
} else if(style().render_style & RENDER_TEXT) {
// also drawing text, use original size
} else {
opts.width = (int) rot.trX(style().width);
opts.height = (int) rot.trY(style().height);
opts.preserve_aspect = (style().alignment & ALIGN_STRETCH) ? ASPECT_STRETCH : ASPECT_FIT;
}
}
}
+3 -2
View File
@@ -23,9 +23,10 @@ class ChoiceValueViewer : public ValueViewer {
virtual bool prepare(RotatedDC& dc);
virtual void draw(RotatedDC& dc);
virtual void onStyleChange(int);
private:
void getOptions(Rotation& rot, GeneratedImage::Options& opts);
};
bool prepare_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value);
void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value);
// ----------------------------------------------------------------------------- : EOF
#endif
+7 -55
View File
@@ -7,6 +7,7 @@
// ----------------------------------------------------------------------------- : Includes
#include <render/value/multiple_choice.hpp>
#include <render/value/choice.hpp>
#include <render/card/viewer.hpp>
#include <data/stylesheet.hpp>
#include <gui/util.hpp>
@@ -15,6 +16,11 @@ DECLARE_TYPEOF_COLLECTION(String);
// ----------------------------------------------------------------------------- : MultipleChoiceValueViewer
bool MultipleChoiceValueViewer::prepare(RotatedDC& dc) {
if (style().render_style & (RENDER_CHECKLIST | RENDER_LIST)) return false;
return prepare_choice_viewer(dc, viewer, style(), value().value());
}
void MultipleChoiceValueViewer::draw(RotatedDC& dc) {
drawFieldBorder(dc);
if (style().render_style & RENDER_HIDDEN) return;
@@ -38,44 +44,7 @@ void MultipleChoiceValueViewer::draw(RotatedDC& dc) {
drawChoice(dc, pos, choice);
}
} else {
// COPY FROM ChoiceValueViewer
if (value().value().empty()) return;
double margin = 0;
if (style().render_style & RENDER_IMAGE) {
// draw image
style().initImage();
CachedScriptableImage& img = style().image;
Context& ctx = viewer.getContext();
ctx.setVariable(SCRIPT_VAR_input, to_script(value().value()));
img.update(ctx);
if (img.isReady()) {
GeneratedImage::Options img_options;
getOptions(dc, img_options);
// Generate image/bitmap
ImageCombine combine = style().combine;
style().loadMask(*viewer.stylesheet);
Bitmap bitmap; Image image;
RealSize size;
img.generateCached(img_options, &style().mask, &combine, &bitmap, &image, &size);
size = dc.trInvS(size);
RealRect rect(align_in_rect(style().alignment, size, dc.getInternalRect()), size);
if (bitmap.Ok()) {
// just draw it
dc.DrawPreRotatedBitmap(bitmap,rect);
} else {
// use combine mode
dc.DrawPreRotatedImage(image,rect,combine);
}
margin = size.width + 1;
}
}
if (style().render_style & RENDER_TEXT) {
// draw text
dc.DrawText(tr(*viewer.stylesheet, value().value(), capitalize(value().value())),
align_in_rect(ALIGN_MIDDLE_LEFT, RealSize(0, dc.GetCharHeight()), dc.getInternalRect()) + RealSize(margin, 0)
);
}
// COPY ENDS HERE
draw_choice_viewer(dc, viewer, style(), value().value());
}
}
@@ -111,20 +80,3 @@ void MultipleChoiceValueViewer::drawChoice(RotatedDC& dc, RealPoint& pos, const
// next position
pos = move_in_direction(style().direction, pos, size, style().spacing);
}
// COPY from ChoiceValueViewer
void MultipleChoiceValueViewer::getOptions(Rotation& rot, GeneratedImage::Options& opts) {
opts.package = viewer.stylesheet.get();
opts.local_package = &getSet();
opts.angle = rot.trAngle(0); //%%
if (nativeLook()) {
opts.width = opts.height = 16;
opts.preserve_aspect = ASPECT_BORDER;
} else if(style().render_style & RENDER_TEXT) {
// also drawing text, use original size
} else {
opts.width = (int) rot.trX(style().width);
opts.height = (int) rot.trY(style().height);
opts.preserve_aspect = (style().alignment & ALIGN_STRETCH) ? ASPECT_STRETCH : ASPECT_FIT;
}
}
+1 -1
View File
@@ -20,12 +20,12 @@ class MultipleChoiceValueViewer : public ValueViewer {
public:
DECLARE_VALUE_VIEWER(MultipleChoice) : ValueViewer(parent,style), item_height(0) {}
virtual bool prepare(RotatedDC& dc);
virtual void draw(RotatedDC& dc);
protected:
double item_height; ///< Height of a single item, or 0 if non uniform
private:
void drawChoice(RotatedDC& dc, RealPoint& pos, const String& choice, bool active = true);
void getOptions(Rotation& rot, GeneratedImage::Options& opts);
};
// ----------------------------------------------------------------------------- : EOF