-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop'
# Conflicts: # readme.md
- Loading branch information
Showing
8 changed files
with
131 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.physmo.garnet; | ||
|
||
import com.physmo.garnet.graphics.Graphics; | ||
|
||
/** | ||
* Interface that both our font classes support | ||
* in order to be used by the paragraph drawer | ||
*/ | ||
public interface DrawableFont { | ||
void drawText(Graphics graphics, String text, int x, int y); | ||
|
||
int getLineHeight(); | ||
|
||
int getStringWidth(String text); | ||
|
||
int getSpaceWidth(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.physmo.garnet; | ||
|
||
|
||
import com.physmo.garnet.graphics.Graphics; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ParagraphDrawer { | ||
DrawableFont font; | ||
int padY = 0; | ||
|
||
public ParagraphDrawer(DrawableFont font) { | ||
this.font = font; | ||
} | ||
|
||
|
||
public void setPadY(int val) { | ||
padY = val; | ||
} | ||
|
||
// TODO: cache analyzed strings. | ||
public void drawParagraph(Graphics g, String text, int width, int height, int x, int y) { | ||
List<WordInfo> wordInfos = analyzeText(font, text); | ||
|
||
int rx = 0; // rolling x | ||
int ry = 0; // rolling y | ||
|
||
for (WordInfo wordInfo : wordInfos) { | ||
if (rx + wordInfo.width > width) { | ||
rx = 0; | ||
ry += font.getLineHeight() + padY; | ||
} | ||
|
||
font.drawText(g, wordInfo.text, x + rx, y + ry); | ||
rx += wordInfo.width; | ||
rx += font.getSpaceWidth(); | ||
} | ||
|
||
} | ||
|
||
|
||
public List<WordInfo> analyzeText(DrawableFont font, String text) { | ||
List<WordInfo> words = new ArrayList<>(); | ||
String[] s = text.split(" "); | ||
for (int i = 0; i < s.length; i++) { | ||
words.add(new WordInfo(s[i], font.getStringWidth(s[i]))); | ||
} | ||
return words; | ||
} | ||
|
||
class WordInfo { | ||
String text; | ||
int width; | ||
|
||
public WordInfo(String _text, int _width) { | ||
text = _text; | ||
width = _width; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters