-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.hs
285 lines (264 loc) · 13.5 KB
/
Editor.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
module DobadoBots.Graphics.Editor(
drawEditor,
renderCode,
handleEditorEvents,
appendEventEditor,
prettyPrintAst,
generateEditorTextures
) where
import Prelude hiding (Left, Right)
import Control.Monad (when, unless)
import Control.Monad.State (StateT(..), get,
modify, liftIO)
import Data.Char (toUpper)
import qualified Data.Text as T (Text(..), unpack,
intercalate, concat,
splitAt, split,
singleton, drop, head,
length, pack, lines, unlines)
import Data.Maybe (listToMaybe, catMaybes)
import Data.Either.Extra (isLeft, isRight, fromLeft')
import SDL.Input.Keyboard.Codes
import qualified SDL (Renderer(..), Point(..),
V2(..), Rectangle(..),
V4(..), Texture(..),
Event(..), EventPayload(..),
KeyboardEventData(..), InputMotion(..),
Keysym(..), KeyModifier(..),
Keycode(..),
fillRect, rendererDrawColor,
copy, TextInputEventData(..))
import SDL (($=))
import Foreign.C.Types (CInt(..))
import qualified SDL.Raw as Raw (Color(..))
import Text.PrettyPrint (Doc(..))
import Text.Parsec (ParseError, sourceColumn, sourceLine,
errorPos)
import DobadoBots.GameEngine.Data (GameState(..), GamePhase(..))
import DobadoBots.Interpreter.Data (Cond(..))
import DobadoBots.Interpreter.PrettyPrinter (prettyPrint)
import DobadoBots.Graphics.Data (RendererState(..), EditorState(..),
EditorEvent(..))
import DobadoBots.Graphics.Utils (loadFontBlended)
generateEditorTextures :: SDL.Renderer -> RendererState -> IO RendererState
generateEditorTextures r rst = do
codeTex <- renderCode r . T.lines . text $ editor rst
if isLeft $ currentParseResult rst
then do
errorTex <- generateSyntaxErrorTex r . fromLeft' $ currentParseResult rst
return $ RendererState
(levels rst)
(currentSelectedLvl rst)
(robotTexture rst)
(editorCursor rst)
codeTex
(running rst)
(editing rst)
(buttons rst)
errorTex
(parseErrorCursor rst)
(splashScreen rst)
(levelNumber rst)
(editor rst)
(currentParseResult rst)
else
return $ RendererState
(levels rst)
(currentSelectedLvl rst)
(robotTexture rst)
(editorCursor rst)
codeTex
(running rst)
(editing rst)
(buttons rst)
(parseErrorMess rst)
(parseErrorCursor rst)
(splashScreen rst)
(levelNumber rst)
(editor rst)
(currentParseResult rst)
offset :: CInt
offset = 14
drawEditor :: SDL.Renderer -> GameState -> RendererState -> IO ()
drawEditor r st rst = do
SDL.rendererDrawColor r $= SDL.V4 0 0 0 0
SDL.fillRect r . Just $ SDL.Rectangle (SDL.P $ SDL.V2 640 0) (SDL.V2 300 480)
if isLeft $ currentParseResult rst
then SDL.rendererDrawColor r $= SDL.V4 255 0 0 0
else SDL.rendererDrawColor r $= SDL.V4 0 255 0 0
when (isLeft $ currentParseResult rst) $ do
SDL.fillRect r . Just . errorCursorPosition . fromLeft' $ currentParseResult rst
SDL.copy r (fst errorCursor) Nothing (Just . errorCursorPosition . fromLeft' $ currentParseResult rst)
SDL.fillRect r . Just $ SDL.Rectangle (SDL.P $ SDL.V2 0 480) (SDL.V2 940 20)
unless (null (parseErrorMess rst) || isRight (currentParseResult rst)) $
SDL.copy r (fst . head $ parseErrorMess rst) Nothing
(Just $ SDL.Rectangle (SDL.P $ SDL.V2 10 481) (snd . head $ parseErrorMess rst))
displayCode r st rst
case phase st of
Running -> SDL.copy r (fst $ running rst) Nothing
(Just $ SDL.Rectangle (SDL.P $ SDL.V2 640 400)(snd $ running rst))
Editing -> SDL.copy r (fst $ editing rst) Nothing
(Just $ SDL.Rectangle (SDL.P $ SDL.V2 640 400)(snd $ editing rst))
_ -> return ()
where
errorCursor = parseErrorCursor rst
errorCursorPosition err = SDL.Rectangle
(SDL.P $ SDL.V2 (640 + (errorColumn err * 6 )) (errorLine err * offset))
(snd $ parseErrorCursor rst)
errorColumn err = fromIntegral $ sourceColumn $ errorPos err
errorLine err = fromIntegral $ sourceLine $ errorPos err
displayCode :: SDL.Renderer -> GameState -> RendererState -> IO ()
displayCode r st rst = do
runStateT (renderLines r $ codeTextures rst) 1
when (phase st == Editing) $ SDL.copy r
(fst $ editorCursor rst)
Nothing
(Just $ SDL.Rectangle cursorPos (snd $ editorCursor rst))
return ()
where
cursorPos = SDL.P $ SDL.V2 (640 + (ccC * 6 )) (clC * offset)
clC = fromIntegral $ cl + 1
ccC = fromIntegral $ cc + 1
(EditorState t cc cl) = editor rst
generateSyntaxErrorTex :: SDL.Renderer -> ParseError -> IO [(SDL.Texture, SDL.V2 CInt)]
generateSyntaxErrorTex r p = mapM (loadFontBlended r "data/fonts/VT323-Regular.ttf" 14 (Raw.Color 255 255 255 0)) [errTxt]
where
errTxt = unwords $ drop 1 errTxtList
errTxtList = lines $ show p
renderCode :: SDL.Renderer -> [T.Text] -> IO [(SDL.Texture, SDL.V2 CInt)]
renderCode r = mapM (renderLine . T.unpack)
where
handleEmptyLines t
| t == "" = " "
| otherwise = t
renderLine l = loadFontBlended r
"data/fonts/VT323-Regular.ttf"
14
(Raw.Color 0 255 0 0)
(handleEmptyLines l)
prettyPrintAst :: Cond -> [T.Text]
prettyPrintAst ast = T.lines $ prettyPrint ast
renderLines :: SDL.Renderer -> [(SDL.Texture, SDL.V2 CInt)] -> StateT CInt IO ()
renderLines r strs = do
mapM_ renderLine strs
return ()
where
renderLine :: (SDL.Texture, SDL.V2 CInt) -> StateT CInt IO ()
renderLine (tex, size) = do
lineNb <- get
modify (+1)
let pos = SDL.P $ SDL.V2 650 (lineNb * offset)
liftIO $ SDL.copy r tex Nothing (Just $ SDL.Rectangle pos size)
handleEditorEvents :: [SDL.Event] -> Maybe EditorEvent
handleEditorEvents evts = listToMaybe . catMaybes $ (Just . handleTextEvent <$> filteredTextInputEvents) ++ (sdlEventTransco <$> filteredKeyboardPressEvents)
where
filteredKeyboardPressEvents = filter filterKeyboardPressEvents (keyBoardPressEventsExtractor <$> filteredKeyboardEventPayloads)
filteredTextInputEvents = textInputEventExtractor <$> filteredTextEventPayloads
keyBoardPressEventsExtractor e = case e of
SDL.KeyboardEvent d -> d
_ -> error
"Problem while filtering keyboard events."
textInputEventExtractor e = case e of
SDL.TextInputEvent d -> d
_ -> error
"Problem while filtering text input event."
filteredTextEventPayloads = filter filterTextEventsPayload eventsPayloads
filteredKeyboardEventPayloads = filter filterKeyboardEventsPayload eventsPayloads
filterTextEventsPayload evt = case evt of
SDL.TextInputEvent _ -> True
_ -> False
filterKeyboardPressEvents e = case SDL.keyboardEventKeyMotion e of
SDL.Pressed -> True
_ -> False
filterKeyboardEventsPayload evt = case evt of
SDL.KeyboardEvent _ -> True
_ -> False
eventsPayloads = map SDL.eventPayload evts
handleTextEvent :: SDL.TextInputEventData -> EditorEvent
handleTextEvent e = AppendChar . T.head $ SDL.textInputEventText e
sdlEventTransco :: SDL.KeyboardEventData -> Maybe EditorEvent
sdlEventTransco (SDL.KeyboardEventData _ _ _ keySym) = case keySym of
(SDL.Keysym _ KeycodeReturn _) -> Just NewLine
(SDL.Keysym _ KeycodeBackspace _) -> Just BackSpace
(SDL.Keysym _ KeycodeDelete _) -> Just Delete
(SDL.Keysym _ KeycodeUp _) -> Just Up
(SDL.Keysym _ KeycodeDown _) -> Just Down
(SDL.Keysym _ KeycodeLeft _) -> Just Left
(SDL.Keysym _ KeycodeRight _) -> Just Right
(SDL.Keysym _ KeycodeSpace _) -> Just Space
(SDL.Keysym _ KeycodeEnd _) -> Just EndOfLine
(SDL.Keysym _ KeycodeHome _) -> Just BeginningOfLine
_ -> Nothing
where
handleCharMods c = if isUpper
then Just $ AppendChar $ toUpper c
else Just $ AppendChar c
code = SDL.keysymKeycode keySym
mods = SDL.keysymModifier keySym
isUpper = SDL.keyModifierLeftShift mods || SDL.keyModifierRightShift mods
appendEventEditor :: EditorEvent -> EditorState -> EditorState
appendEventEditor (AppendChar c) est@(EditorState t cc cl) = EditorState (insertCharEditor c est) (cc + 1) cl
appendEventEditor NewLine est@(EditorState t cc cl) = EditorState (insertCharEditor '\n' est) 0 (cl+1)
appendEventEditor Space est = appendEventEditor (AppendChar ' ') est
appendEventEditor Delete est@(EditorState t cc cl) = EditorState (removeChar est) cc cl
appendEventEditor BackSpace est@(EditorState t cc cl)
| cc == 0 && cl > 0 = EditorState ( sucklessUnlines . removeLine cl $ appendLineToPreviousLine cl splittedLines) endPreviousLine (cl - 1)
| cc > 0 = EditorState (removeChar $ EditorState t (cc - 1) cl) (max (cc-1) 0) cl
| otherwise = est
where
sucklessUnlines = T.intercalate (T.singleton '\n')
splittedLines = T.split (=='\n') t
customUnlines = T.intercalate (T.singleton '\n')
endPreviousLine = getLineLength (cl - 1) t
appendEventEditor Left (EditorState t cc cl) = EditorState t (max (cc - 1) 0) cl
appendEventEditor Right (EditorState t cc cl) = EditorState t (min (cc + 1) lineLength) cl
where
lineLength = getLineLength cl t
appendEventEditor Up est@(EditorState t cc cl)
| cl == 0 = est
| otherwise = EditorState t (min cc upperLineLength) (max 0 (cl - 1))
where
upperLineLength = getLineLength (cl - 1) t
appendEventEditor Down est@(EditorState t cc cl)
| cl == endLine = est
| otherwise = EditorState t (min cc downLineLength) (min endLine (cl + 1))
where
endLine = length (T.split (=='\n') t) - 1
downLineLength = getLineLength (cl + 1) t
appendEventEditor EndOfLine (EditorState t cc cl) = EditorState t (getLineLength cl t) cl
appendEventEditor BeginningOfLine (EditorState t cc cl) = EditorState t 0 cl
getLineLength :: Int -> T.Text -> Int
getLineLength i t = T.length $ T.split (=='\n') t !! i
insertCharEditor :: Char -> EditorState -> T.Text
insertCharEditor c (EditorState t cc cl)
| t == "" = T.singleton c
| otherwise = T.intercalate (T.singleton '\n') newLines
where
newLines = insertAt cl alteredLine editorLines
alteredLine = T.concat [fst splittedLine, T.singleton c, snd splittedLine]
splittedLine = T.splitAt cc insertLine
insertLine = editorLines !! cl
editorLines = T.split (=='\n') t
removeChar :: EditorState -> T.Text
removeChar (EditorState t cc cl)
| T.length t < 2 = ""
| otherwise = T.intercalate (T.singleton '\n') newLines
where
newLines = insertAt cl alteredLine editorLines
alteredLine = T.concat [fst splittedLine, T.drop 1 $ snd splittedLine]
splittedLine = T.splitAt cc deleteLine
deleteLine = editorLines !! cl
editorLines = T.split (=='\n') t
insertAt :: Int -> T.Text -> [T.Text] -> [T.Text]
insertAt i y xs
| length xs > 1 = as ++ (y:bs)
| otherwise = [y]
where (as,tr:bs) = splitAt i xs
removeLine :: Int -> [T.Text] -> [T.Text]
removeLine pos xs = take pos xs ++ drop (pos + 1) xs
appendLineToPreviousLine :: Int -> [T.Text] -> [T.Text]
appendLineToPreviousLine pos xs = insertAt (pos - 1) (T.concat (previousLine : [line])) xs
where line = xs !! pos
previousLine = xs !! (pos - 1)